当前位置:   article > 正文

Cannot deserialize value of type `java.time.LocalDateTime` from String

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

在接口的测试中遇到了如下错误,该如何解决呢?

Cannot deserialize value of type java.time.LocalDateTime from String

或者

Cannot deserialize value of type java.time.LocalDate from String


在实体类中的参数类型为LocalDateTime,而在用Swagger或者Postman进行测试的时候,传递的是Json类型的数据,进而传递过去的参数是字符串类型的时间。
解决方法:在接收参数的实体类对应接收日期的属性加上以下注解
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")


@ApiModelProperty(value = "开始时间")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private LocalDateTime beginTime;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

使用此注解需要引入Jackson的依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.12.0</version>
</dependency>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

一切搞定!!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/813801
推荐阅读
相关标签
  

闽ICP备14008679号