SpringMvc使用注解的形式解决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;
 
public class SpringMVC_Custom_Json_Date_Deserializer extends JsonDeserializer {
	
	@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;
	}
	
	
 
}

自测时,发现该方法在get请求的时候.无法实现Date类型转换的效果..post请求是可以

你可能感兴趣的:(SpringMvc使用注解的形式解决Date类型映射错误)