Michael Rutherfurd
Michael Rutherfurd

Reputation: 14045

How do I customise jaxb generation?

I have a series of xml messages, all with their own schemas and namespaces. The messages are currently marshalled using JAXB (we still live in a Java 1.4 environment) and we have a large amount of legacy code using this JAXB code so any solution needs to be minimally intrusive.

My problem is that while each of the messages has a set of common header tags the namespace covers the entire message and therefore makes the header for each message unique as well. As a result the "common" header sections are loaded into the namespace bound versions using a common class that is implemented using very ugly proxy classes and dynamic reflection code. This common class has been identified as the source of some performance issues.

Ideally I want to implement a replacement using the following:

Unfortunately, I don't have control of the message structure otherwise I would look at creating a seperate "header" namespace. I thought of running an XSLT transform to "rename" the header namespace after marshalling and prior to unmarshalling but I would prefer to avoid the extra load if possible, even if it means a more complex build.

Is what I want to do feasible or have I missed something fundamental? Is there any hints as to how to implement? Plugin versions etc?

Addendum 1: Binding using javaType would do the job but it appears that doesn't work for complex types.

Addendum 2: Binding using class almost does it as well but I would want it to specify a specific class and package so I could ignore the generated duplicates.

Upvotes: 3

Views: 1705

Answers (1)

janko
janko

Reputation: 4203

JAXB 2.x has a @XmlJavaTypeAdapter annotation which may be the solution to your problem (see this blog by Kohsuke Kawaguchi).

You can map your common header class to the generated namespace-specific header classes with an implementation of XmlAdapter<XMLHeaderFromNamespaceX, CommonHeader> and use the adapter with XmlJavaTypeAdapter.

However, as a downside you would need an adapter for each of your particular namespaces.

Upvotes: 1

Related Questions