Over a million developers have joined DZone.
Platinum Partner

How To Dynamically Change a Web Service Endpoint URL with Metro

· Java Zone

The Java Zone is brought to you in partnership with AppDynamics. Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems.

A really cool feature of GlassFish Metro (and maybe other webservice stacks) is the ability to change the webservice endpoint URL at runtime.

In general, you typically generate the code via JAX-WS maven plugin (or ant task), specifying the endpoint URL in the generation properties. This is a fast and reliable way to generate webservices. The downside is, that it seems you can't change the endpoint URL at runtime ...
Well, you can. And it is pretty simple.

The example in this posting is taken from the Metro FAQ.

You can use BindingProvider.ENDPOINT_ADDRESS_PROPERTY to set the endpoint address in your client application code.

Sample:

 //Create service and proxy from the generated Service class.   
  HelloService service = new HelloService();   
  HelloPort proxy = service.getHelloPort();   
  ((BindingProvider)proxy).getRequestContext().  
       put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://new/endpointaddress");   
     
  proxy.sayHello("Hello World!");  
With the property key

BindingProvider.ENDPOINT_ADDRESS_PROPERTY
you can also get the currently used endpoint url at runtime. This may be helpful for debugging purposes.

Sample:
 LOG.info("Using endpoint URL: "  
        + ((BindingProvider)proxy).getRequestContext().get(  
            BindingProvider.ENDPOINT_ADDRESS_PROPERTY)); 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

Published at DZone with permission of Ralf Quebbemann , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}