java—如何使用rest控制器将localdatetime反序列化为localdate获取响应中的值是localdatetime,dto中的值是localdate?

jqjz2hbq  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(431)

我发现了很多关于将localdate转换/反序列化为localdatetime的信息,但是没有在localdate中找到localdatetime的信息,在我们使用rest请求的情况下,响应包含格式为localdatetime的值,并且写入该值的dto具有localdate。
从响应体填充的数据

@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;
}

示例按响应体显示的外观(为了获得最佳读数,删除了一些其他值)

{
    "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"
        },
    ]
}

我该犯的错误;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

我试过的:
@jsonparser注解
@日期格式注解
我想尝试一下:
Jackson的定制转换器?
自定义反序列化程序?
谢谢!

x33g5p2x

x33g5p2x1#

尝试使用以下注解:

@JsonSerialize(using = LocalDateSerializer.class) 
@JsonFormat(pattern="yyyy-MM-dd")

相关问题