Set Endpoint Address in JAX-WS Client

  1.  
  2. try {
  3. HelloService service = new HelloService (
  4. new URL("http://new/endpointaddress?wsdl"),
  5. new QName("http://example.org/hello", "HelloService "));
  6. } catch (MalformedURLException e) {
  7. log.fatal(e);
  8. }
  9.  
  10. HelloPort proxy = service.getHelloPort();
  11. proxy.sayHello("Hello World!");
  12.  

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.

  1.  
  2. //Create service and proxy from the generated Service class.
  3. HelloService service = new HelloService();
  4. HelloPort proxy = service.getHelloPort();
  5.  
  6. Map<String, Object> ctxt = ((BindingProvider)proxy ).getRequestContext();
  7. ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192);
  8. ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://new/endpointaddress");
  9.  
  10. proxy.sayHello("Hello World!");
  11.  

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.

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

One Response to Set Endpoint Address in JAX-WS Client

  1. Ping says:

    Hi Jianming,

    This is a good discussion. Is there a way to suppress the HTTP GET for WSDL?

    Thanks,

    Ping

Leave a Reply

Your email address will not be published.


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>