码迷,mamicode.com
首页 > 编程语言 > 详细

spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava

时间:2014-10-22 21:47:05      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   ar   java   for   sp   

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}



spring mvc3中JACKSON序列化日期格式的问题 - 墙头草的Java - BlogJava

标签:style   blog   http   io   os   ar   java   for   sp   

原文地址:http://www.cnblogs.com/wang3680/p/490b618d004dafcc0a7555001c82312d.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!