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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/preferG/article/details/78035830

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


猜你喜欢

转载自blog.csdn.net/preferG/article/details/78035830