关于Spring Jackson 反序列化Date时遇到的问题
来源:互联网 发布:warframe对网络要求 编辑:程序博客网 时间:2024/06/03 20:36
Jackson对于date的反序列化只支持几种,如果不符合默认格式则会报一下错误
具体支持:("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")
异常信息:
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver 2017-09-19 23:10:12 -- WARN -- Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of java.util.Date from String value '2012-12-01 12:01:00': not a valid representation (error: Failed to parse Date value '2012-12-01 12:01:00': Can not parse date "2012-12-01 12:01:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) at [Source: java.io.PushbackInputStream@1b56b734; line: 1, column: 46] (through reference chain: com.cy.ssm.beans.UserBean["book"]->com.cy.ssm.beans.book["date_"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2012-12-01 12:01:00': not a valid representation (error: Failed to parse Date value '2012-12-01 12:01:00': Can not parse date "2012-12-01 12:01:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) at [Source: java.io.PushbackInputStream@1b56b734; line: 1, column: 46] (through reference chain: com.cy.ssm.beans.UserBean["book"]->com.cy.ssm.beans.book["date_"])org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver 2017-09-19 23:10:12 -- WARN -- Handler execution resulted in exception: Could not read document: Can not construct instance of java.util.Date from String value '2012-12-01 12:01:00': not a valid representation (error: Failed to parse Date value '2012-12-01 12:01:00': Can not parse date "2012-12-01 12:01:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) at [Source: java.io.PushbackInputStream@1b56b734; line: 1, column: 46] (through reference chain: com.cy.ssm.beans.UserBean["book"]->com.cy.ssm.beans.book["date_"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2012-12-01 12:01:00': not a valid representation (error: Failed to parse Date value '2012-12-01 12:01:00': Can not parse date "2012-12-01 12:01:00": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) at [Source: java.io.PushbackInputStream@1b56b734; line: 1, column: 46] (through reference chain: com.cy.ssm.beans.UserBean["book"]->com.cy.ssm.beans.book["date_"])
解决方案:
import java.io.IOException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import com.fasterxml.jackson.core.JsonParser;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.DeserializationContext;import com.fasterxml.jackson.databind.JsonDeserializer;/** * * 项目名称: * 类名称:SpringMVC_Custom_Json_Date_Deserializer * 类描述: * 创建人: * 创建时间:2017年9月19日 下午11:15:52 * 修改人:Administrator * 修改时间:2017年9月19日 下午11:15:52 * 修改备注: * @version * */public class SpringMVC_Custom_Json_Date_Deserializer extends JsonDeserializer<Date> {@Override public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = jp.getText(); try { return format.parse(date); } catch (ParseException e) { throw new RuntimeException(e); } } }
必要的操作:
在对应的实体类的setting上进行注册
@JsonDeserialize(using = SpringMVC_Custom_Json_Date_Deserializer.class)
具体注册实力:
import java.util.Date;import com.cy.ssm.util.SpringMVC_Custom_Json_Date_Deserializer;import com.fasterxml.jackson.databind.annotation.JsonDeserialize;public class book {private int id;private String name;private String page;private Date date_;public Date getDate_() {return date_;}@JsonDeserialize(using = SpringMVC_Custom_Json_Date_Deserializer.class) public void setDate_(Date date_) {this.date_ = date_;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPage() {return page;}public void setPage(String page) {this.page = page;}}
重启服务器。
还有一种方式是使用
org.springframework.format.annotation包下的@DateTimeFormat(pattern = "yyyy-MM-dd HHmmss") 也是可以接收Date类型
阅读全文
0 0
- Spring Jackson 反序列化Date时遇到的问题
- Spring Jackson 反序列化Date时遇到的问题
- Spring Jackson 反序列化Date时遇到的问题
- 关于Spring Jackson 反序列化Date时遇到的问题
- Jackson 反序列化Date时遇到的问题
- Spring Jackson 反序列化Date时遇到的问题(http://www.aichengxu.com/java/81030.htm)
- Jackson 反序列化问题
- Jackson反序列化问题
- Jackson反序列化的实现方法
- Spring boot 配置jackson 反序列化时间格式、忽略实体中没有的属性
- json 反序列化的时候遇到的问题整理
- XStream反序列化xml时遇到汉字乱码问题
- jackson的序列化和反序列化的实现
- jackson的序列化和反序列化
- Json_JackSon_lesson1 Jackson的 序列化与反序列化
- Jackson实现json的序列化反序列化
- Jackson多态类型数据的反序列化
- json-jackson多子类继承的反序列化
- 头插法创建单链表代码实现
- 字符串
- 万年历的编写
- android多线程-AsyncTask之工作原理深入解析(下)
- C++中类的对象和指针的区别理解
- 关于Spring Jackson 反序列化Date时遇到的问题
- C语言——字符串指针-strcmp
- HDU6215 Brute Force Sorting 【模拟】
- dubbo的使用及配置
- tarjan算法求割点
- 新阶段,勇攀高峰!
- android stdio 之 jdk1.8
- 序列化、反序列化:serializable
- Android 之 Handler ,Looper机制详解