在接口的测试中遇到了如下错误,该如何解决呢?
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;
使用此注解需要引入Jackson的依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.0</version>
</dependency>
一切搞定!!!