In spring mvc3, JACKSON mentioned related date problems in serialization of date formats (http://jackyrong.iteye.com/admin/blogs/1089909. Similarly, if the REST format is returned in JAXB, the problem also exists. Let's continue with the example:
1) POJO class:
Package com. loiane. model;
Import java. util. Date;
Public class Company {
Private int id;
Private String company;
Private double price;
Private double change;
Private double pctChange;
Private Date lastChange;
// Getters and setters
2) Now we want to return the company set and use JAXB to wrap it.
Import java. util. List;
Import javax. xml. bind. annotation. XmlElement;
Import javax. xml. bind. annotation. XmlRootElement;
@ XmlRootElement (name = "companies ")
Public class Companies {
@ XmlElement (required = true)
Private List <Company> list;
Public void setList (List <Company> list ){
This. list = list;
}
}
In spring controller, return the following:
@ RequestMapping (value = "/company/view. action ")
Public @ ResponseBody Companies view () throws Exception {}
The returned XML is as follows:
<Companies>
<List>
<Change> 0.02 </change>
<Company> 3 m Co </company>
<Id> 1 </id>
<LastChange> 2011-09-01T00: 00: 00-0:00 </lastChange>
<PctChange> 0.03 </pctChange>
<Price> 71.72 </price>
</List>
<List>
<Change> 0.42 </change>
<Company> Alcoa Inc </company>
<Id> 2 </id>
<LastChange> 2011-09-01T00: 00: 00-0:00 </lastChange>
<PctChange> 1.47 </pctChange>
<Price> 29.01 </price>
</List>
</Companies>
We can see that the time format is not very good, so we can customize the format:
Package com. loiane. util;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Import javax. xml. bind. annotation. adapters. XmlAdapter;
Public class JaxbDateSerializer extends XmlAdapter <String, Date> {
Private SimpleDateFormat dateFormat = new SimpleDateFormat ("MM-dd-yyyy ");
@ Override
Public String marshal (Date date) throws Exception {
Return dateFormat. format (date );
}
@ Override
Public Date unmarshal (String date) throws Exception {
Return dateFormat. parse (date );
}
}
Here, the XmlAdapter is inherited and both marshal and unmarshal are carried out, that is, string date and date conversion. When using it, note that the @ XmlJavaTypeAdapter label is added, as shown below:
Import java. util. Date;
Import javax. xml. bind. annotation. adapters. XmlJavaTypeAdapter;
Import com. loiane. util. JaxbDateSerializer;
Public class Company {
Private int id;
Private String company;
Private double price;
Private double change;
Private double pctChange;
Private Date lastChange;
@ XmlJavaTypeAdapter (JaxbDateSerializer. class)
Public Date getLastChange (){
Return lastChange;
}
// Getters and setters
}
The result is output correctly;
<Companies>
<List>
<Change> 0.02 </change>
<Company> 3 m Co </company>
<Id> 1 </id>
<LastChange> 09-01-2011 </lastChange>
<PctChange> 0.03 </pctChange>
<Price> 71.72 </price>
</List>
<List>
<Change> 0.42 </change>
<Company> Alcoa Inc </company>
<Id> 2 </id>
<LastChange> 09-01-2011 </lastChange>
<PctChange> 1.47 </pctChange>
<Price> 29.01 </price>
</List>
</Companies>