首页 > 代码库 > spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava
spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava
<style type="text/css" id="wiz_custom_css">body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif; font-size: 10.5pt; line-height: 1.5;}html, body{ }h1 { font-size:1.5em; font-weight:bold;}h2 { font-size:1.4em; font-weight:bold;}h3 { font-size:1.3em; font-weight:bold;}h4 { font-size:1.2em; font-weight:bold;}h5 { font-size:1.1em; font-weight:bold;}h6 { font-size:1.0em; font-weight:bold;}img { border:0; max-width: 100%;}blockquote { margin-top:0px; margin-bottom:0px;}table { border-collapse:collapse; border:1px solid #bbbbbb;}td { border-collapse:collapse; border:1px solid #bbbbbb;}</style>spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava 在SPRING MVC3中,比如做REST时,经常要用JASON去解析,遇到了日期格式化的问题,
请看例子:
1) 比如有一个POJO
? package com.loiane.model;
import java.util.Date;
import org.codehaus.jackson.annotate.JsonAutoDetect;
@JsonAutoDetect
@Entity
public class Company {
??? private int id;
??? private double price;
??? private String company;
??? private Date date;
??? private String size;
??? private byte visible;
}
2)在controller中,返回一个MAP
?? public @ResponseBody Map<String,List<Company>> view() throws Exception
3) 于是SPIRNG MVC返回如下的JSON字符:
?? {"total":27,"data":[{"price":71.72,"company":"3m Co","visible":1,"id":1,"size":"large","date":1188615600000},{"price":29.01,"company":"Aloca
Inc","visible":0,"id":2,"size":"medium","date":1185937200000},{"price":83.81,"company":"Altria Group
Inc","visible":0,"id":3,"size":"large","date":1186110000000
。。。。。。。。。。。。。。。。
4) 可以看到,返回的时间格式不大好看
5)于是,可以这样设置:
?? @JsonSerialize(using=JsonDateSerializer.class)
public Date getDate() {
??? return date;
}
其中?? JsonDateSerializer是新编写的类,继承了JsonSerializer
? import java.io.IOException;集装箱运费
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
??? private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
??? @Override
??? public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
??????????? throws IOException, JsonProcessingException {
??????? String formattedDate = dateFormat.format(date);
??????? gen.writeString(formattedDate);
??? }
}
?? 很简单,格式化一下,这下输出JSON就可以了
{"total":27,"data":[{"price":71.72,"company":"3m Co","visible":1,"id":1,"size":"large","date":"09-01-2007"},{"price":29.01,"company":"Aloca
Inc","visible":0,"id":2,"size":"medium","date":"08-01-2007"},{"price":83.81,"company":"Altria Group
Inc","visible":0,"id":3,"size":"large","date":"08-03-2007"},{"price":52.55,"company":"American Express Company","visible":1,"id":4,"size":"extraTP LINK路由器无线信号能传送多少米
large","date":"01-04-2008"},{"price":64.13,"company":"American International Group
Inc.","visible":1,"id":5,"size":"small","date":"03-04-2008"},{"price":31.61,"company":"AT&T Inc.","visible":0,"id":6,"size":"extra
large","date":"02-01-2008"},{"price":75.43,"company":"Boeing Co.","visible":1,"id":7,"size":"large","date":"01-01-2008"},{"price":67.27,"company":"Caterpillar
Inc.","visible":1,"id":8,"size":"medium","date":"12-03-2007"},{"price":49.37,"company":"Citigroup,
Inc.","visible":1,"id":9,"size":"large","date":"11-24-2007"},{"price":40.48,"company":"E.I. du Pont de Nemours and Company","visible":0,"id":10,"size":"extra
large","date":"05-09-2007"}],"success":true}
请看例子:
1) 比如有一个POJO
? package com.loiane.model;
import java.util.Date;
import org.codehaus.jackson.annotate.JsonAutoDetect;
@JsonAutoDetect
@Entity
public class Company {
??? private int id;
??? private double price;
??? private String company;
??? private Date date;
??? private String size;
??? private byte visible;
}
2)在controller中,返回一个MAP
?? public @ResponseBody Map<String,List<Company>> view() throws Exception
3) 于是SPIRNG MVC返回如下的JSON字符:
?? {"total":27,"data":[{"price":71.72,"company":"3m Co","visible":1,"id":1,"size":"large","date":1188615600000},{"price":29.01,"company":"Aloca
Inc","visible":0,"id":2,"size":"medium","date":1185937200000},{"price":83.81,"company":"Altria Group
Inc","visible":0,"id":3,"size":"large","date":1186110000000
。。。。。。。。。。。。。。。。
4) 可以看到,返回的时间格式不大好看
5)于是,可以这样设置:
?? @JsonSerialize(using=JsonDateSerializer.class)
public Date getDate() {
??? return date;
}
其中?? JsonDateSerializer是新编写的类,继承了JsonSerializer
? import java.io.IOException;集装箱运费
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.stereotype.Component;
@Component
public class JsonDateSerializer extends JsonSerializer<Date>{
??? private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
??? @Override
??? public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
??????????? throws IOException, JsonProcessingException {
??????? String formattedDate = dateFormat.format(date);
??????? gen.writeString(formattedDate);
??? }
}
?? 很简单,格式化一下,这下输出JSON就可以了
{"total":27,"data":[{"price":71.72,"company":"3m Co","visible":1,"id":1,"size":"large","date":"09-01-2007"},{"price":29.01,"company":"Aloca
Inc","visible":0,"id":2,"size":"medium","date":"08-01-2007"},{"price":83.81,"company":"Altria Group
Inc","visible":0,"id":3,"size":"large","date":"08-03-2007"},{"price":52.55,"company":"American Express Company","visible":1,"id":4,"size":"extraTP LINK路由器无线信号能传送多少米
large","date":"01-04-2008"},{"price":64.13,"company":"American International Group
Inc.","visible":1,"id":5,"size":"small","date":"03-04-2008"},{"price":31.61,"company":"AT&T Inc.","visible":0,"id":6,"size":"extra
large","date":"02-01-2008"},{"price":75.43,"company":"Boeing Co.","visible":1,"id":7,"size":"large","date":"01-01-2008"},{"price":67.27,"company":"Caterpillar
Inc.","visible":1,"id":8,"size":"medium","date":"12-03-2007"},{"price":49.37,"company":"Citigroup,
Inc.","visible":1,"id":9,"size":"large","date":"11-24-2007"},{"price":40.48,"company":"E.I. du Pont de Nemours and Company","visible":0,"id":10,"size":"extra
large","date":"05-09-2007"}],"success":true}
来自为知笔记(Wiz)
spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。