Jackson serialized date format problem in spring MVC3-Java-blogjava of the wall grass

Source: Internet
Author: User
Tags dateformat

<title>Jackson serialized date format problem in spring MVC3-Java-blogjava of the wall grass</title>In spring MVC3, for example, when you do rest, you often use Jason to parse and encounter date formatting problems.
Take a look at the example:

1) For example, there is a 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) in the controller, return a map
?? Public @ResponseBody map<string,list<company>> View () throws Exception

3) SPIRNG MVC then 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 returned 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; container freight
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);
??? }

}

?? Very simple, formatted, this output JSON can be
{"Total": +, "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 router wireless signal can transmit how many meters
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 ":" 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 ": Ten," Size ":" Extra
Large "," date ":" 05-09-2007 "}]," Success ": true}

From for notes (Wiz)

Jackson serialized date format problem in spring MVC3-Java-blogjava of the wall grass

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.