关于Spring Jackson 反序列化Date时遇到的问题

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类型


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值