views:

29

answers:

2

I'm trying configure a "system wide" custom javax.xml.bind.annotation.adapters.XmlAdapter for the java.util.Locale type in Jersey. It's easy enough to use @XmlJavaTypeAdapter on classes I control but that's not always the case (3rd party code that I can't annotate).

It seems like it would be a pretty common problem but I can't find any good examples or doco on how to handle it.

So, is it possible?

Thanks!

A: 

I can see three possible options:

  1. Register the converter with the marshaller with setAdapter(). You can have a static builder function which adds all your 'system level' type adapters to all marshallers which you use in your application. It all depends on your definition of 'system level'
  2. Use a delegate
  3. Do some fancy bytecode trickery to add the annotations to existing class files.

My advice would be to use approach 1, which is simple and straightforward.

disown
Thanks for the response. I managed to get it going using a package level `@XmlJavaTypeAdapter` annotation.I had to a look at the `setAdapter()` method. The javadoc seems to suggest that it still requires the `@XmlJavaTypeAdapter`...?
danw
A: 

If you need to annotate classes you can't modify, you could always use the externalized metadata feature of EclipseLink JAXB (MOXy).

The metadata file would look something like:

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"&gt;
    <java-types>
        <java-type name="java.util.Locale">
            <xml-java-type-adapter value="some.package.YourAdapter"/>
        </java-type>
    </java-types>
</xml-bindings>

To you EclipseLink MOXy you need to add a jaxb.properties file in with your model classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Blaise Doughan