Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'creatTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat @io.swagger.v3.oas.annotations.media.Schema @javax.validation.constraints.NotNull java.util.Date] for value [["2023-06-01 00:00:00","2023-06-17 00:00:00"]]; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [["2023-06-01 00:00:00","2023-06-17 00:00:00"]]怎么解决这个报错
时间: 2024-03-21 21:38:52 浏览: 818
这个报错是因为在将字符串类型的 creatTime 属性转换为 List<Date> 类型时出现了错误。可以尝试使用自定义的 Converter 或者 Formatter 来实现类型转换。具体方法如下:
1. 创建一个实现 Converter 接口的类,重写 convert 方法,将字符串类型的 creatTime 属性转换为 List<Date> 类型。
```
public class StringToListConverter implements Converter<String, List<Date>> {
@Override
public List<Date> convert(String source) {
List<Date> dateList = new ArrayList<>();
try {
ObjectMapper objectMapper = new ObjectMapper();
dateFormat.setLenient(false);
objectMapper.setDateFormat(dateFormat);
dateList = objectMapper.readValue(source, new TypeReference<List<Date>>(){});
} catch (Exception e) {
e.printStackTrace();
}
return dateList;
}
}
```
2. 在 Spring 配置文件中配置该 Converter。
```
<mvc:annotation-driven>
<mvc:converters>
<bean class="com.example.StringToListConverter"/>
</mvc:converters>
</mvc:annotation-driven>
```
这样就可以将 creatTime 属性从字符串类型转换为 List<Date> 类型了。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![0.jar](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)