[英]How i can deserialize LocalDateTime into LocalDate using a REST controller GET where value in response is LocalDateTime, value in DTO is LocalDate?
I found a lot of information about converting / deserializing LocalDate to LocalDateTime, but did not find about LocalDateTime in LocalDate, in the case when we use a REST request, where the response contains a value with the format LocalDateTime and the DTO where this value is written has LocalDate.我发现了很多关于将 LocalDate 转换/反序列化为 LocalDateTime 的信息,但在 LocalDate 中没有找到有关 LocalDateTime 的信息,在我们使用 REST 请求的情况下,其中响应包含格式为 LocalDateTime 的值和该值所在的 DTO写有LocalDate。
DTO which fill from response body从响应主体填充的 DTO
@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;
}
Example how looks by response body (some other values was deleted for best reading)示例响应正文的外观(为了最佳阅读,删除了一些其他值)
{
"result": [
{
"date": "2015-11-16T00:00:00+03:00",
"begin_date": "2015-11-16T00:00:00+03:00",
"end_date": "2025-04-30T00:00:00+03:00"
},
]
}
Error which i've deserve;D我应得的错误;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
What i've tried:我试过的:
What i think to try:我想尝试什么:
Thanks a Lot!多谢!
Try to use this annotations:尝试使用此注释:
@JsonSerialize(using = LocalDateSerializer.class)
@JsonFormat(pattern="yyyy-MM-dd")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.