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

Advertisements

About johnnywey

Welcome to A Regular Expression. This blog is designed to reflect my thoughts on life, music, software design, Apple, faith, philosophy, and whatever else I can think of.

Posted on April 9, 2009, in Java, Programming. Bookmark the permalink. 9 Comments.

  1. Also, I needed to add the extraarg -autoNameResolution.

  2. I meant: <extendedSoapHeaders>true</extendedSoapHeaders>

  3. This didn’t work for me. Any other suggestions?

  4. It didn’t add the soap headers to the methods as parameters. I wound up adding them to the operation in the wsdl myself. Obviously, not the preferred method. But, I think this took care of my soap header problem.

    Now I am getting ‘Unsupported Content-Type: text/xml; charset=utf-8 Supported ones are [application/soap+xml]’. Have you seen this exception before?

  5. 1000 times thank you!!!!!

  6. Thanks a lot for this, this really came in handy!!

  7. thumbs up, finally that does the trick also in grails 2.4.4 with cxf

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

%d bloggers like this: