项目总结44:SpringBoot接口时间数据Json格式转化异常MappingJackson2HttpMessageConverter
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了项目总结44:SpringBoot接口时间数据Json格式转化异常MappingJackson2HttpMessageConverter,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3850字,纯文字阅读大概需要6分钟。
内容图文
![项目总结44:SpringBoot接口时间数据Json格式转化异常MappingJackson2HttpMessageConverter](/upload/InfoBanner/zyjiaocheng/1130/ff6c9f4f074d46ad8726c68a86aa015f.jpg)
项目总结44:SpringBoot接口时间数据Json格式转化异常MappingJackson2HttpMessageConverter
项目中,通过接口传数据,报json解析异常,异常日志如下:
2019 09:37:27 WARN com.hs.web.common.exception.GlobalExceptionHandler - org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize value of type java.sql.Timestamp from String "2019-11-22 11:35:00": not a valid representation (error: Failed to parse Date value ‘2019-11-22 11:35:00‘: Can not parse date "2019-11-22 11:35:00": while it seems to fit format ‘yyyy-MM-dd‘T‘HH:mm:ss.SSS‘, parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.sql.Timestamp from String "2019-11-22 11:35:00": not a valid representation (error: Failed to parse Date value ‘2019-11-22 11:35:00‘: Can not parse date "2019-11-22 11:35:00": while it seems to fit format ‘yyyy-MM-dd‘T‘HH:mm:ss.SSS‘, parsing fails (leniency? null)) at [Source: java.io.PushbackInputStream@27cf9ee4; line: 1, column: 44] (through reference chain: com.hs.dao.entity.xijiang.match.Match["timeStart"])
从日志中,发现时间数据"2019-11-22 11:35:00"无法匹配默认的“yyyy-MM-dd‘T‘HH:mm:ss.SSS”格式;导致解析失败;
解决方案是自定义事件转换需要匹配的格式,这里需要匹配的格式是"yyyy-MM-dd HH:mm:ss";
第一步,自定义DateFormat:
package com.hs.common.other; import java.text.*; import java.util.Date; publicclass MyDateFormat extends DateFormat{ privatestaticfinallong serialVersionUID = 1L; private DateFormat dateFormat; private SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public MyDateFormat(DateFormat dateFormat) { this.dateFormat = dateFormat; } @Override public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) { return dateFormat.format(date, toAppendTo, fieldPosition); } @Override public Date parse(String source, ParsePosition pos) { Date date = null; try { date = format1.parse(source, pos); } catch (Exception e) { date = dateFormat.parse(source, pos); } return date; } // 主要还是装饰这个方法 @Override public Date parse(String source) throws ParseException { Date date = null; try { // 先按我的规则来 date = format1.parse(source); } catch (Exception e) { // 不行,那就按原先的规则吧 date = dateFormat.parse(source); } return date; } // 这里装饰clone方法的原因是因为clone方法在jackson中也有用到 @Override public Object clone() { Object format = dateFormat.clone(); returnnew MyDateFormat((DateFormat) format); } }
第二步:自定义SpringBoot配置类,加载被修饰的MappingJackson2HttpMessageConverter:
package com.hs.web.boot.configuration; import com.fasterxml.jackson.databind.ObjectMapper; import com.hs.common.other.MyDateFormat; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import java.text.DateFormat; /** * * * @author tyj *本配置目的是为了解决前端传时间yyy-MM-dd HH:mm:ss json解析错误问题 */ @Configuration public class MappingJackson2HttpMessageConverterConfig { @Autowired private Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder; @Bean public MappingJackson2HttpMessageConverter MappingJsonpHttpMessageConverter() { ObjectMapper mapper = jackson2ObjectMapperBuilder.build(); // ObjectMapper为了保障线程安全性,里面的配置类都是一个不可变的对象 // 所以这里的setDateFormat的内部原理其实是创建了一个新的配置类 DateFormat dateFormat = mapper.getDateFormat(); mapper.setDateFormat(new MyDateFormat(dateFormat)); MappingJackson2HttpMessageConverter mappingJsonpHttpMessageConverter = new MappingJackson2HttpMessageConverter( mapper); return mappingJsonpHttpMessageConverter; } }
原文:https://www.cnblogs.com/wobuchifanqie/p/11870750.html
内容总结
以上是互联网集市为您收集整理的项目总结44:SpringBoot接口时间数据Json格式转化异常MappingJackson2HttpMessageConverter全部内容,希望文章能够帮你解决项目总结44:SpringBoot接口时间数据Json格式转化异常MappingJackson2HttpMessageConverter所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。