Avoid generation of WS client with JAXBElement<T>

Goal

Avoid generation of WS client with JAXBElement

Description

This recipe explains how to create a JAXB binding file to avoid generating a WS client using wsimport from exposing methods using JAXBElement types (notice, however, that if you need to use them or want to use them, you may use the ObjectFactory class that was generated together with the remaining client code to create the appropriate JAXBElement elements).

How to

I will assume that you are using a maven project here and so the recipe will explain the necessary configuration steps for a maven like project.

First, create the binding file, naming it binding.xjb and put it in src/bindings:

<jaxb:bindings version="2.1" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <jaxb:globalBindings generateElementProperty="false" />
</jaxb:bindings>

Next, you need to configure the jaxws-maven-plugin to use the bindings file:

<plugin>
  <groupId>org.jvnet.jax-ws-commons</groupId>
  <artifactId>jaxws-maven-plugin</artifactId>
  <version>2.2.1</version>
  <configuration>
    <bindingFiles>
      <bindingFile>${basedir}/src/bindings/binding.xjb</bindingFile>
    </bindingFiles>
  </configuration>
  ...
</plugin>

Explanations

And that’s it. The previous configurations is what is necessary to avoid exposing JAXBElement type methods. Notice also that the configuration we made with maven forces the wsimport command to use the -b flag, similarly to:

wsimport -keep -s ./generated-sources -d target/classes -Xnocompile \
-wsdllocation http://<wsdlURL>?wsdl -b src/bindings/binding.xjb \
file:/src/wsdl/MyWSDL.wsdl

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s