首页 > 代码库 > SpringMVC返回Json,自定义Json中Date类型格式
SpringMVC返回Json,自定义Json中Date类型格式
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 的格式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。