Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime';
Text '2021-04-08 05:55:41.934Z' could not be parsed at index 2
LocalDateTime类型的数据给前端,前端再返回来就出现了类型转换的问题
前端穿过来的字段是:2021-04-08T05:55:41.934
下面是我的接口
@GetMapping(value = {"getMsgList/{userId}/{pageSize}/{lastDate}","getMsgList/{userId}/{pageSize}"})
public Result getMsgList(@PathVariable Integer userId,
@PathVariable Integer pageSize,
@PathVariable(required = false) LocalDateTime lastDate){
return websocketService.getMsgList(userId,lastDate,pageSize);
}
目前能想到的是有两种解决方式,
第一种是,不用LocalDateTime接收数据,直接用String接收数据,接收之后再转成LocalDateTime类型的数据
第二种的增加配置类,虽然第一种也可以,但是我就是不太想用String类型接收时间
下面是配置类
package com.maiya.community.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import java.time.LocalDateTime;
/**
* 接口传时间类型参数转换
*/
@Configuration
public class DateConfig {
@Bean
public Converter<String, LocalDateTime> localDateTimeConverter(){
return new Converter<String, LocalDateTime>() {
@Override
public LocalDateTime convert(String s) {
return LocalDateTime.parse(s);
}
};
}
}
加了这个配置类之后,如果想接收localDateTime类型的数据的话,就会先走配置类,把前端传过来的数据转成LocalDateTime类型的数据,
这样就不会出现类型接收异常的问题了
方法是可行的,但是要根据前端传过来的具体时间方式来进行处理,处理是活的