How to add SOAP headers to a SOAP request using JAX-WS?

ikvenu2000 picture ikvenu2000 · Sep 12, 2012 · Viewed 27.7k times · Source

We need to consume webservices developed by other team. Using JAX-WS for generating the webservices. We are using wsimport to generate the client side stubs.

The problem is that i need to pass the following info as a header along with the SOAP body:

<soapenv:Header>
    <ns1:HeaderData xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" 
                    xmlns:ns1="http://www.example.com/esb/data_type/HeaderData/v1">
        <ChannelIdentifier>ABC</ChannelIdentifier>
    </ns1:HeaderData>
</soapenv:Header>


We are using:

BindingProvider.getRequestContext().put(
    BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
    serviceConfig.getServiceEndPoint()
);

to set the endpoint.

Can anyone suggest how to pass headers with request?

Thanks, VK

Answer

Ilya picture Ilya · Sep 12, 2012

use param header = true of @WebParam annotation

@WebMethod
@WebResult String method(
      @WebParam String anotherParam
      @WebParam(header = true, mode = Mode.OUT) Holder<String> headerParam)  

header = true, mode = Mode.OUT means that param headerParam will be only in output in header.
If you want this this param in input and in output, make Mode.INOUT