Skip to main content

How to override the service address ?

If you are using JAXWS API to create the proxy obejct, here is an example which is complete JAX-WS compliant code

URL wsdlURL = MyService.class.getClassLoader.getResource ("myService.wsdl");
QName serviceName = new QName("urn:myService", "MyService");
MyService service = new MyService(wsdlURL, serviceName);
ServicePort client = service.getServicePort();
BindingProvider provider = (BindingProvider)client;
// You can set the address per request here
provider.getRequestContext().put(
   BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
   "http://my/new/url/to/the/service");

If you are using CXF ProxyFactoryBean to create the proxy object , you can do like this

JaxWsProxyFactoryBean proxyFactory = new JaxWsProxyFactoryBean();
proxyFactory.setServiceClass(ServicePort.class);
// you could set the service address with this method
proxyFactory.setAddress("theUrlyouwant");
ServicePort client = (ServicePort) proxyFactory.create();

Here is another way which takes advantage of JAXWS's Service.addPort() API

URL wsdlURL = MyService.class.getClassLoader.getResource(
   "service2.wsdl");
QName serviceName = new QName("urn:service2", "MyService");
QName portName = new QName("urn:service2", "ServicePort");
MyService service = new MyService(wsdlURL, serviceName);
// You can add whatever address as you want
service.addPort(portName, "http://schemas.xmlsoap.org/soap/", 
   "http://the/new/url/myService");
// Passing the SEI class that is generated by wsdl2java      
ServicePort proxy = service.getPort(portName, SEI.class);

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!