Java 如何使用REST控制器将LocalDateTime反序列化为LocalDate获取响应中的值为LocalDateTime,DTO中的值为LocalDate?
我找到了很多关于将LocalDate转换/反序列化为LocalDateTime的信息,但在LocalDate中没有找到LocalDateTime的信息,在使用REST请求时,响应包含一个格式为LocalDateTime的值,并且写入该值的DTO具有LocalDate 从响应体填充的DTOJava 如何使用REST控制器将LocalDateTime反序列化为LocalDate获取响应中的值为LocalDateTime,DTO中的值为LocalDate?,java,spring,serialization,annotations,converters,Java,Spring,Serialization,Annotations,Converters,我找到了很多关于将LocalDate转换/反序列化为LocalDateTime的信息,但在LocalDate中没有找到LocalDateTime的信息,在使用REST请求时,响应包含一个格式为LocalDateTime的值,并且写入该值的DTO具有LocalDate 从响应体填充的DTO @Data @NoArgsConstructor @AllArgsConstructor public class ContractDto { @JsonFormat(pattern="yyy
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ContractDto {
@JsonFormat(pattern="yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
@JsonProperty(value = "date")
private LocalDate date;
@JsonFormat(pattern="yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
@JsonProperty(value = "begin_date")
private LocalDate beginDate;
@JsonFormat(pattern="yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
@JsonProperty(value = "end_date")
private LocalDate endDate;
}
示例响应主体的外观(为了获得最佳读数,删除了一些其他值)
错误是我应得的;D
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.RestClientException: Error while extracting response for type [ru.tfm.connector.model.dto.ResultDto<ru.tfm.transport.model.dto.connector.ContractDto>] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String "2015-11-16T00:00:00+03:00": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2015-11-16T00:00:00+03:00' could not be parsed, unparsed text found at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2015-11-16T00:00:00+03:00": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2015-11-16T00:00:00+03:00' could not be parsed, unparsed text found at index 10
at [Source: (PushbackInputStream); line: 6, column: 12] (through reference chain: ru.tfm.connector.model.dto.ResultDto["result"]->java.util.ArrayList[0]->ru.tfm.transport.model.dto.connector.ContractDto["date"])] with root cause
java.time.format.DateTimeParseException: Text '2015-11-16T00:00:00+03:00' could not be parsed, unparsed text found at index 10
路径为[]的上下文中Servlet[dispatcherServlet]的Servlet.service()引发异常[请求处理失败;嵌套异常为org.springframework.web.client.RestClientException:提取类型[ru.tfm.connector.model.dto.ResultDto]和内容类型[application/json]的响应时出错;嵌套异常为org.springframework.http.converter.httpMessageNodeTableException:JSON解析错误:无法从字符串“2015-11-16T00:00:00+03:00”反序列化“java.time.format.DateTimeParseException”类型的值:未能反序列化java.time.LocalDate:(java.time.format.DateTimeParseException)无法解析文本“2015-11-16T00:00:00+03:00”,在索引10处找到未解析的文本;嵌套异常为com.fasterxml.jackson.databind.exc.InvalidFormatException:无法从字符串“2015-11-16T00:00:00+03:00”反序列化“java.time.LocalDate”类型的值:未能反序列化java.time.LocalDate:(java.time.format.DateTimeParseException)无法分析文本“2015-11-16T00:00:00+03:00”,在索引10处找到未分析的文本
在[Source:(PushbackInputStream);第6行,第12列](通过引用链:ru.tfm.connector.model.dto.ResultTo[“result”]->java.util.ArrayList[0]->ru.tfm.transport.model.dto.connector.ContractDto[“date”]),并带有根本原因
java.time.format.DateTimeParseException:无法分析文本“2015-11-16T00:00:00+03:00”,在索引10处找到未分析的文本
我所尝试的:
非常感谢 尝试使用以下注释:
@JsonSerialize(using = LocalDateSerializer.class)
@JsonFormat(pattern="yyyy-MM-dd")
不幸的是,它不起作用
@JsonSerialize(using = LocalDateSerializer.class)
@JsonFormat(pattern="yyyy-MM-dd")