java.time.format.DateTimeParseException: Text '2019-07-31T05:08:18.416Z' could not be parsed at inde

java.time.format.DateTimeParseException: Text ‘2019-07-31T05:08:18.416Z’ could not be parsed at inde
前提背景是这样的 。 一个spring boot 的项目 ,配置了 FastJsonHttpMessageConverter 。

配置:

@Bean
    public HttpMessageConverters fastJsonConfigure(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, 
              SerializerFeature.WriteNullStringAsEmpty,
              SerializerFeature.WriteMapNullValue,
              SerializerFeature.WriteNullListAsEmpty,
              SerializerFeature.WriteNullNumberAsZero);
        converter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(converter);
    }

然后 发现 Controller 里的参数 , LocalDateTime 类型的 在反序列化的时候 报错:

入参:

public class WorkReq implements Serializable{
    private static final long serialVersionUID = -7724964763310381651L;
   
    @JSONField( format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    private LocalDateTime endTimePage;
}

错误信息:

Caused by: com.alibaba.fastjson.JSONException: Text '2019-07-31T05:08:18.416Z' could not be parsed at index 23
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:665)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:365)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:333)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:296)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:402)
	at com.alibaba.fastjson.JSON.parseObject(JSON.java:484)
	at com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter.readType(FastJsonHttpMessageConverter.java:203)
	... 68 common frames omitted
Caused by: java.time.format.DateTimeParseException: Text '2019-07-31T05:08:18.416Z' could not be parsed at index 23
	at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
	at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
	at java.time.LocalDateTime.parse(LocalDateTime.java:492)
	at com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec.parseDateTime(Jdk8DateCodec.java:231)
	at com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec.deserialze(Jdk8DateCodec.java:86)
	at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:80)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:745)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:271)
	at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:267)
	at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:661)
	... 74 common frames omitted

就是这个样子了 。

前端参数:

{
  "endTimePage": "2019-07-31T05:08:18.416Z",
  "startTimePage": "2019-07-31T05:08:18.416Z"
}

开始上网搜索,其中两篇文章对我帮助很大
FastJsonHttpMessageConverter设置dateFormat后导致JSONField注解format失效解决办法:https://blog.csdn.net/zxmsdyz/article/details/51680699

Spring Mvc使用Jackson进行json转对象时,遇到的字符串转日期的异常处理(Can not deserialize value of type Date from String):https://blog.csdn.net/qq906627950/article/details/79503801

最终 结合着文章 又看看源码 使得问题得以解决

 @JSONField( format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    private LocalDateTime endTimePage;

这样写 就可以了 。还有人说是版本问题 我的fastjson 版本是 1.2.47 已经很高了

你可能感兴趣的:(java8)