task #2637
openCheck JodaTime and JAXB
0%
Description
Check if JAXB works correctly with JodaTime.
The following came with the JodaTime mailing list:
I was converting some code to use a DateTime instead of a Date and a property has an @XmlElement annotation. Having apparently stumbled on to a web service, some Googling eventually found me http://blog.bdoughan.com/2011/05/jaxb-and-joda-time-dates-and-times.html
Is that kind of thing presently the recommended approach? Is there some semi-standard library we should be using or should I just roll my own marshaller?
Mark
======
I've never used JAXB myself, nor have I heard of anyone doing an integration library, so that blog post is probably the state of the art.
Stephen
======
The blog post covers the basics. If you don't control the Web Service and so can't add your annotations then there's a technique where you use "xjb" to modify the generated source that you create using wsimport.
One issue you will typically find is that there isn't really a "standard" when it comes to webservices - every webservice seems to be ever so slightly different to each other. Date Times with or without time and time zone is often a point of different despite the the "standards".
---XeroCustomBindings.xjb
<?xml version="1.0" encoding="UTF-8"?> <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"> <!-- Using bindings xjc -classpath /Users/rperfect/IdeaProjects/DataZime/XeroClient/build/classes -p com.xero.domain -b XeroCustomBindings.xjb -d gen-src *.xsd --> <jaxb:globalBindings> <jaxb:javaType name="org.joda.time.DateTime" xmlType="xs:date" parseMethod="com.datazime.connector.xero.client.DateAdapter.parseDate" printMethod="com.datazime.connector.xero.client.DateAdapter.printDate" /> <jaxb:javaType name="org.joda.time.DateTime" xmlType="xs:dateTime" parseMethod="com.datazime.connector.xero.client.DateAdapter.parseDatetim e" printMethod="com.datazime.connector.xero.client.DateAdapter.printDatetim e" /> <xjc:simple /> </jaxb:globalBindings> </jaxb:bindings>
package com.datazime.connector.xero.client; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; /** */ public class DateAdapter { private static final DateTimeFormatter XML_DATE_FORMAT = ISODateTimeFormat.dateTimeNoMillis(); private static final DateTimeFormatter XML_DATE_TIME_FORMAT = ISODateTimeFormat.localDateOptionalTimeParser(); private static final DateTimeFormatter DATE_PATTERN = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"); public static DateTime parseDatetime(String dateStr) { DateTime result = XML_DATE_TIME_FORMAT.parseDateTime(dateStr); return result; } public static String printDatetime(DateTime date) { String result = DATE_PATTERN.print(date); return result; } public static DateTime parseDate(String dateStr) { DateTime result = XML_DATE_FORMAT.parseDateTime(dateStr); return result; } public static String printDate(DateTime date) { String result = DATE_PATTERN.print(date); return result; } }