0
点赞
收藏
分享

微信扫一扫

【解决报错】Cannot deserialize value of type `java.time.LocalDate` from String “2020-1-2“:

yundejia 2022-04-23 阅读 59
java

报错如下:

Bad Request: JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String "2020-1-2": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2020-1-2' could not be parsed at index 5; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2020-1-2": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2020-1-2' could not be parsed at index 5
 at [Source: (PushbackInputStream); line: 8, column: 15] (through reference chain: com.ledar.mono.domain.Patient["birthday"])

java 中 Instant 类型的数据在 swagger 上进行测试时 以json 格式输入时格式为2018-07-09T08:59:08.853Z
java 中 LocalDate 类型的数据在 swagger 上进行测试时 以json 格式输入时格式为2018-07-09,需要特别注意的是,07和09是两位数字,不是一位数字。

如果导包正确的话(Jackson的包,将String 与JSON格式正反序列化,互相转换),不需要作如下配置

 /**
     * 出生日期
     */
    @Schema(description = "出生日期")
    @Column(name = "birthday")
    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "yyyy-MM-dd")
    private LocalDate birthday;
    
举报

相关推荐

0 条评论