In spring MVC3, for example, when doing rest, you often have to parse with Jason and encounter the problem of date formatting.
Take a look at the example:
1 For example, there is a Pojo
Package Com.loiane.model;
private int id;
private double price;
Private String Company;
private date date;
private String size;
private byte visible;
}
2 in controller, returns a map
Public @ResponseBody map<string,list<company>> View () throws Exception
3) So SPIRNG MVC returns the following JSON characters:
{"Total": "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) You can see that the return time format is not very good
5 So, you can set this:
@JsonSerialize (Using=jsondateserializer.class)
Public Date getDate () {
return date;
}
Where Jsondateserializer is a newly-written class that inherits the 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");
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.