http://www.cnblogs.com/jsczljh/p/3654636.html
————————————————————————————————————————————————————————————
SpringMVC返回Json,自定义Json中Date类型格式
SpringMVC返回Json数据依赖jackson这个开源的第三方类库。
若不加任何说明情况下Date类型将以时间戳的形式转换为Json并返回。
jackson提供了一些自定义格式的方法。我们只需继承它的抽象类JsonSerializer<T> ,并在指定的属性方法上添加注解@JsonSerialize即可实现。
1.编写Date转换类(yyyy-MM-dd)
public class CustomDateSerializer extends JsonSerializer<Date>
{
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)throws IOException, JsonProcessingException
{
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = format.format(date);
gen.writeString(formattedDate);
}
}
{
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)throws IOException, JsonProcessingException
{
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = format.format(date);
gen.writeString(formattedDate);
}
}
2.javabean中对应属性的get()方法添加注解
package com.easyui.model;
import java.util.Date;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.springframework.format.annotation.DateTimeFormat;
import com.easyui.utils.CustomDateSerializer;
public class User {
private Integer id;
private String username;
private String password;
private String realname;
private String sex;
private Date birthday;
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@JsonSerialize(using=CustomDateSerializer.class)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
import java.util.Date;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.springframework.format.annotation.DateTimeFormat;
import com.easyui.utils.CustomDateSerializer;
public class User {
private Integer id;
private String username;
private String password;
private String realname;
private String sex;
private Date birthday;
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@JsonSerialize(using=CustomDateSerializer.class)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
至此,就可以实现返回的JSON数据中日期格式自动转换为:yyyy-MM-dd 的格式