Set Endpoint Address in JAX-WS Client

try {
  HelloService service = new HelloService (
      new URL("http://new/endpointaddress?wsdl"), 
      new QName("http://example.org/hello", "HelloService "));
} catch (MalformedURLException e) {
  log.fatal(e);
}
 
HelloPort proxy = service.getHelloPort();
proxy.sayHello("Hello World!");

You can also use BindingProvider.ENDPOINT_ADDRESS_PROPERTY to override endpoint address. One caveat is the original endpoint used to generated the client proxy need to be up, otherwise you’ll get a nasty “java.net.ConnectException: Connection refused” exception when instantiating the Service at the first place.

//Create service and proxy from the generated Service class.
HelloService service = new HelloService();
HelloPort proxy = service.getHelloPort();
 
Map<String, Object> ctxt = ((BindingProvider)proxy ).getRequestContext();
ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192);
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://new/endpointaddress");
 
proxy.sayHello("Hello World!");

Use a local wsdl placed in classpath to create service and port, then set new end point address. This solves the issue that the original wsdl can NOT be be obtained from a live server and the live wsdl has a different service name, for example as a result of service virtualization.

HelloService service = new HelloService (
      this.getClass().getResource("originalHello.wsdl"), 
      new QName("http://example.org/hello", "HelloService "));
HelloPort proxy = service.getHelloPort();
 
Map<String, Object> ctxt = ((BindingProvider)proxy ).getRequestContext();
ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192);
ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://new/endpointaddress");
 
proxy.sayHello("Hello World!");
This entry was posted in jax-ws. Bookmark the permalink.

One Response to Set Endpoint Address in JAX-WS Client

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.