DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > JAXB Custom Binding – Java.util.Date / Spring 3 Serialization

JAXB Custom Binding – Java.util.Date / Spring 3 Serialization

Loiane Groner user avatar by
Loiane Groner
·
Jun. 08, 11 · Java Zone · Interview
Like (0)
Save
Tweet
25.20K Views

Join the DZone community and get the full member experience.

Join For Free

jaxb can handle java.util.date serialization, but it expects the following format: “yyyy-mm-ddthh:mm:ss “. what if you need to format the date object in another format?

i had the same issue when i was working with spring mvc 3 and jackson json processor , and recently, i faced the same issue working with spring mvc 3 and jaxb for xml serialization .

let’s dig into the issue:

problem:

i have the following java beans which i want to serialize in xml using spring mvc 3:

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

and i have another object which is going to wrap the pojo above:

package com.loiane.model;

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 my spring controller, i’m going to return a list of company through the the @responsebody annotation – which is going to serialize the object automatically with jaxb:

@requestmapping(value="/company/view.action")
public @responsebody companies view() throws exception {}

when i call the controller method, this is what it returns to the view:

<companies>
<list>
<change>0.02</change>
<company>3m co</company>
<id>1</id>
<lastchange>2011-09-01t00:00:00-03: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-03:00</lastchange>
<pctchange>1.47</pctchange>
<price>29.01</price>
</list>
</companies>

note the date format. it is not the format i expect it to return. i need to serialize the date in the following format: “ mm-dd-yyyy “

solution:

i need to create a class extending the xmladapter and override the marshal and unmarshal methods and in these methods i am going to format the date as i need to:

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);
}
}

and in my java bean class, i simply need to add the @xmljavatypeadapter annotation in the get method of the date property.

package com.loiane.model;

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
}

if we try to call the controller method again, it is going to return the following xml:

<companies>
<list>
<change>0.02</change>
<company>3m 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>

problem solved!

happy coding! :)

from http://loianegroner.com/2011/06/jaxb-custom-binding-java-util-date-spring-3-serialization/

Spring Framework Serialization Binding (linguistics)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Role of Development Team in an Agile Environment
  • Implementing RBAC Configuration for Kubernetes Applications
  • Unit Vs Integration Testing: What's the Difference?
  • Why I'm Choosing Pulumi Over Terraform

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo