当前位置:   article > 正文

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

cannot deserialize value of type `java.time.localdate` from string

报错如下:

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"])

  • 1
  • 2
  • 3

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;
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/813794
推荐阅读
相关标签
  

闽ICP备14008679号