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

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 digg 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-yyyySolution:

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! icon smile JAXB Custom Binding   Java.util.Date / Spring 3 Serialization

Reference: JAXB Custom Binding – Java.util.Date / Spring 3 Serialization from our JCG partner Loiane Groner at the Loiane Groner’s blog blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


+ seven = 8



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books