DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Rule-Based Prompts: How To Streamline Error Handling and Boost Team Efficiency With ChatGPT
  • An Overview of Cloud Cryptography
  • Grow Your Skills With Low-Code Automation Tools
  • The Role of Automation in Streamlining DevOps Processes

Trending

  • Rule-Based Prompts: How To Streamline Error Handling and Boost Team Efficiency With ChatGPT
  • An Overview of Cloud Cryptography
  • Grow Your Skills With Low-Code Automation Tools
  • The Role of Automation in Streamlining DevOps Processes
  1. DZone
  2. Coding
  3. Frameworks
  4. 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 · Interview
Like (0)
Save
Tweet
Share
25.71K 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.

Trending

  • Rule-Based Prompts: How To Streamline Error Handling and Boost Team Efficiency With ChatGPT
  • An Overview of Cloud Cryptography
  • Grow Your Skills With Low-Code Automation Tools
  • The Role of Automation in Streamlining DevOps Processes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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

Let's be friends: