How to Call a SOAP Service with WS-Addressing with the JAX-WS Dispatch API

import java.net.URL;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;
import javax.xml.ws.soap.AddressingFeature;

public final class JaxWsSample {

  public static void main(final String[] args) throws Exception {
    final Service service = Service.create(new URL("http://localhost/jaxwssample/SampleService?WSDL"), new QName("http://fernandoribeiro.eti.br/sample/jaxws", "SampleService"));

    final Dispatch dispatch = service.createDispatch(new QName("http://fernandoribeiro.eti.br/sample/jaxws", "SamplePort"), Source.class /* or JAXBContext */, Mode.PAYLOAD /* or Mode.MESSAGE */, new AddressingFeature() /* enabled and non-required by default */);

    final BindingProvider provider = (BindingProvider) dispatch;

    final Map<String, Object> requestContext = provider.getRequestContext();

    requestContext.put(BindingProvider.SOAPACTION_URI_PROPERTY, "http://fernandoribeiro.eti.br/sample/jaxws/SampleService/Sample"); /* the action URI of the input message of the operation to be invoked */

    dispatch.invoke(...); /* or invokeAsync or invokeOneWay */
  }

}

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