Set Endpoint Address in JAX-WS Client
Example:
1 | try { |
2 | HelloService service = new HelloService ( |
3 | new URL("http://new/endpointaddress?wsdl"), |
4 | new QName("http://example.org/hello", "HelloService ")); |
5 | } catch (MalformedURLException e) { |
6 | log.fatal(e); |
7 | } |
8 | |
9 | HelloPort proxy = service.getHelloPort(); |
10 | proxy.sayHello("Hello World!"); |
11 |
You can 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 | //Create service and proxy from the generated Service class. |
2 | HelloService service = new HelloService(); |
3 | HelloPort proxy = service.getHelloPort(); |
4 | |
5 | Map<String, Object> ctxt = ((BindingProvider)proxy ).getRequestContext(); |
6 | ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192); |
7 | ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://new/endpointaddress"); |
8 | |
9 | proxy.sayHello("Hello World!"); |
10 |
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 | HelloService service = new HelloService ( |
2 | this.getClass().getResource("originalHello.wsdl"), |
3 | new QName("http://example.org/hello", "HelloService ")); |
4 | |
5 | Map<String, Object> ctxt = ((BindingProvider)proxy ).getRequestContext(); |
6 | ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192); |
7 | ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://new/endpointaddress"); |
8 | |
9 | proxy.sayHello("Hello World!"); |
10 |
Declaration:
This doc is a copy from http://jianmingli.com/wp/?p=585