Skip to main content

TOTD #2: Change the endpoint address on a pre-generated Web services Stub

Posted by arungupta on August 17, 2007 at 1:20 PM PDT

The client-side stubs generated by
JAX-WS (part of
Metro) are pre-configured with the
endpoint address specified in the WSDL. This allows you to invoke the endpoint
directly without any additional configuration. However there may be a need to
send the exact same Web service request to an endpoint hosted at a different
address. For example, the stubs may be generated from a WSDL hosted in the test
environment and the request needs to be sent to an endpoint hosted in production
environment that is very likely to be on a different machine.

This tip tells you how to change the endpoint address on a
pre-generated stub so that it invokes a different endpoint.

The code to invoke a pre-configured stub (using the endpoint
address from the WSDL) looks like:

Hello port = new HelloService().getHelloPort();

String result = port.sayHello("Duke!");

As per the

JAX-WS 2.1 specification
, each proxy implements

javax.xml.ws.BindingProvider
interface. In order to invoke an endpoint whose
address is different from the one specified in the WSDL, you need to insert the
following line between the two lines shown above:

((javax.xml.ws.BindingProvider)port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, 
"NEW_ADDRESS_HERE");

Just replace NEW_ADDRESS_HERE with your new endpoint address and
the exact same request will be directed to the new endpoint. The WSDL contract
must be exactly same though otherwise you may get an exception back.

Please leave suggestions on TOTD that you'd like to see. A
complete archive is available
here
.

Technorati:
totd
webservices
projectmetro

soap
glassfish

Related Topics >>