1. 程式人生 > >關於Spring Jackson 反序列化Date時遇到的問題

關於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: 
[email protected]
; 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:
[email protected]
; 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:
[email protected]
; 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: [email protected]; 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型別