前端Get请求时间报错endTime Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'endTime'

使用注解形式解决ConversionFailedException

Posted by Autuan.Yu on June 5, 2023

问题描述

使用Get请求时请求时,路径&参数如下: http://localhost:8080/meber/query?startTime=2023-05-07T09:21:50.830Z&endTime=2023-06-05T09:21:50.830Z

报错如下:

endTime Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'endTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@io.swagger.annotations.ApiModelProperty java.time.LocalDateTime] for value '2023-06-05T09:21:50.830Z'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-06-05T09:21:50.830Z]

解决方式:

使用@DateTimeFormat 注解来解决

public class MemberQueryParam {  
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)  
    private LocalDateTime orderStartTime;  
      
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)  
    private LocalDateTime orderEndTime;  

    // ... 省略其他属性和方法
}