Simple SOAP Webservice Client with CXF

I’m sure this has probably been documented many times, so this is more for my own documentation than anything, so here goes:

  1. Download Apache CXF. Make sure to get the binaries not the source version.
  2. Unpack the download and put it somewhere on disk (C:\Program Files, \opt, etc)
  3. Add the path to the bin folder to your OS path.
  4. Run the wsdl2java script:
wsdl2java -client -fe jaxws21 -d C:\Users\mynamehere\Desktop\generated-webservice-client http://someservername/someservice.svc?wsdl

Here is the documentation to the tool:
http://cxf.apache.org/docs/wsdl-to-java.html

A couple of notes:

  1. You don’t really have to have the ‘-client’ param, it just gives a starting point for client code.
  2. If you’re using < java 7, you absolutely need the ‘-fe jaxws21’ param, as it will generate code that will not compile for java 5 & 6.

UPDATE #1:

I recently used this and was getting this error:
“WSDLToJava Error: Thrown by JAXB : A class/interface with the same name “*************” is already in use. Use a class customization to resolve this conflict.”

A quick google search landed me here.

By adding this flag: “-autoNameResolution” I got it to work, so here is my updated command:

wsdl2java -autoNameResolution -client -fe jaxws21 -d C:\Users\mynamehere\Desktop\generated-webservice-client http://someservername/someservice.svc?wsdl

UPDATE #2:

After another client generation I opened up the new source files to find that many of the fields in them were

JAXBElement<String>

A quick google search turned up this helpful stackoverflow post.

As the solution to that question suggests, save a file bindings.txt with this in it:

<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>

The updated script now should look something like this:

wsdl2java -b path-to-bindings.txt -autoNameResolution -client -fe jaxws21 -d C:\Users\mynamehere\Desktop\generated-webservice-client http://someservername/someservice.svc?wsdl

Leave a comment