Java. util. date in jaxb binding

Source: Internet
Author: User
Tags dateformat

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>

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.