Adding SOAP Headers to CXF Calls
Recently, I consumed a WSDL with Apache CXF that required authentication information to be placed into a SOAP Header rather than through standard HTTP authentication. This is all well and good, but I couldn’t figure out how to attach the headers to the CXF method calls. The authentication object had been generated by CXF, but there was no way to attach the object to the other methods.
After some research, I found the answer. Adding
-exsh true to WSDL2JAVA updated the method signatures to include the header object as a valid parameter. If you’re cool like me and use the CXF Maven code generator plugin, the XML looks like this:
<execution> <id>generate-sources-vixxi</id> <phase>generate-sources</phase> <configuration> <sourceRoot>target/generated-sources</sourceRoot> <wsdlOptions> <wsdlOption> <extraargs> <extraarg>-exsh</extraarg> <extraarg>true</extraarg> </extraargs> <wsdl>yourWSDLPathHere</wsdl> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution>
I verified this works with CXF 2.0.9 and 2.1.4. I haven’t tested it on any other versions, but I don’t see any reason why it wouldn’t work on 2.0.9+.