Setting the target endpoint URL on Java SOAP web service clients
What I learned today — 28 February 2018
When integrating a Java application with a SOAP web service it is often necessary to set the URL of the endpoint on the target system. One example is to use a test instance of the remote system in your own testing environment.
Passing the wsdl URL in the constructor causes the wsdl to be downloaded and parsed again
Web service clients are often instantiated with a URL parameter:
ExampleService service = new ExampleService(new URL("test env url"));
- This downloads and parses the URL at the point of instantiation. That may be an expensive operation and should preferably only be done once.
- This will fail if the remote system is not available.
Passing the wsdl URL in the constructor DOES NOT change the endpoint where requests will be sent
It is a common mistake to pass the wsdl URL and to think that it changes the address where requests are sent. To change the endpoint address, set the ENDPOINT_ADDRESS_PROPERTY
:
ExampleService service = new ExampleService();
IExampleService iService = service.getWSHttpBindingIExampleService();
BindingProvider bp = (WSBindingProvider) iService;
bp.getRequestContext().put(WSBindingProvider.ENDPOINT_ADDRESS_PROPERTY, "test env url");
Avoid downloading the wsdl
If the web service client is generated with a fixed version of the wsdl, perhaps checked into source code, it is not desirable to download and parse the wsdl when instantiating the client. If you generated the client using CXF
or wsimport
the wsdl location should already be on the class path. Instantiating the client with it’s no-arguments contructor (or by passing a null
URL parameter) will load the wsdl from the wsdl location on the classpath used during generation and not download it again.
Reference: https://stackoverflow.com/a/19827446/297331