JAXB, Custom bindings, Adapter1.class and Joda-time

I was in a WSDL first context: no java at all, just generate a CXF Client from a provided WSDL.

I was stuck with the ugly Adapter1.java for a long time, but I found the solution there.

You will use a custom XMLAdapter like already explained.

The key of this problem was adding the xjc extension to the global binding file:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
 xmlns:xs="http://www.w3.org/2001/XMLSchema" 
 xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
 jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.1">
    <jaxb:globalBindings>
       <xjc:javaType adapter="com.xxx.tools.xjc.DateAdapter" 
        name="java.util.Date" xmlType="xs:dateTime" />
    </jaxb:globalBindings>
</jaxb:bindings>

xjc extension allow the usage of xjc:javaType that accept adapter parameter. No more static method required !

Note this seems to work with jaxb 2.1+ only.


You do not need to extend XmlAdapter and with Joda-Time v2, you do not even need to implement static methods, as they are already provided.

<jaxb:javaType xmlns="http://java.sun.com/xml/ns/jaxb" 
    name="org.joda.time.LocalDate"
    xmlType="xs:date"
    parseMethod="org.joda.time.LocalDate.parse"
    printMethod="java.lang.String.valueOf"
/>

See JAXB datatype converters for xs:date xs:time and xs:dateTime


You do not need to extend XmlAdapter.

Just create static methods on a POJO and it will work.

Example:

 public class DateAdapter {
    private static DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyyMMdd");

    public static LocalDate unmarshal(String v) throws Exception {
        return fmt.parseLocalDate(v);
    }

    public static String marshal(LocalDate v) throws Exception {
        return v.toString("yyyyMMdd");
    }
 }