当前位置:   article > 正文

时间日期JSON解析器

时间日期JSON解析器

后端报错: 

2024-02-14 16:33:44.715 ERROR 968 --- [nio-8080-exec-8] c.pxl.exception.GlobalExceptionHandler   : runtimeException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)": not a valid representation (error: Failed to parse Date value 'Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)': Cannot parse date "Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)": not a valid representation (error: Failed to parse Date value 'Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)': Cannot parse date "Thu Feb 22 2024 00:00:00 GMT+0800 (中国标准时间)": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd"))

前端返回过来的数据无法解析JSON格式,可以配置一个日期反序列化类进行配置。

具体类型需要看你要什么再来详细配置 

  1. package com.pxl.config;
  2. import com.fasterxml.jackson.core.JsonParser;
  3. import com.fasterxml.jackson.databind.DeserializationContext;
  4. import com.fasterxml.jackson.databind.JsonDeserializer;
  5. import java.io.IOException;
  6. import java.sql.Date;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. // 自定义日期反序列化类
  10. public class CustomDateDeserializer extends JsonDeserializer<Date> {
  11. private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
  12. @Override
  13. public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, IOException {
  14. String dateStr = jsonParser.getText();
  15. try {
  16. return (Date) dateFormat.parse(dateStr);
  17. } catch (ParseException e) {
  18. throw new RuntimeException("Failed to parse date: " + dateStr, e);
  19. }
  20. }
  21. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/107322
推荐阅读
相关标签
  

闽ICP备14008679号