FaqWebServiceLocation

How can I change URL the web service client connects to at runtime

JAX-WS client (Java EE 5)

Using BindingProvider.ENDPOINT_ADDRESS_PROPERTY:

    ...
    //get an instance of port
    hello.client.HelloPort port = service.getHelloPort();
    //change the wsdl location
    ((BindingProvider) port).getRequestContext().put(
        BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
        "http://new.server.com/some/path/toWs?wsdl");
    ...

Using Web Service injection (@WebServiceRef)

Inside the Java EE 5 container the wsdl url can be specified in @WebServicRef annotation :

public class ClientServlet extends HttpServlet {
@WebServiceRef(wsdlLocation = "http://localhost:8080/WebApplication1/Hello?wsdl")
private hello.client.HelloService service;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       ...
       hello.client.HelloPort port = service.getHelloPort();
       ....

    }
    ...
}

Specifying WSDL Url in web.xml

Inside the web container (e.g. in servlet) the WSDL URL can be specified more dynamicaly - in deployment descriptor:

<web-app ...>
    <context-param>
        <param-name>wsdl-url</param-name>
        <param-value>http://localhost:8080/WebApplication1/Hello?wsdl</param-value>
    </context-param>
    ...
</web-app>

then, the previous code ca be modified in servlet class:

public class ClientServlet extends HttpServlet {
    ...
    //get an instance of port
    hello.client.HelloPort port = service.getHelloPort();
    //change the wsdl location
    ((BindingProvider) port).getRequestContext().put(
        BindingProvider.ENDPOINT_ADDRESS_PROPERTY,getWsdlUrl());
    ...

    private String getWsdlUrl() {
        return getServletContext().getInitParameter("wsdl-url");
    }
}

JAX-RPC client (J2EE 1.4)

    ...
    //get an instance of port
    test.CounterService port = service.getCounterServicePort();
    //change the wsdl location
    ((javax.xml.rpc.Stub) port)._setProperty(
        javax.xml.rpc.Stub.ENDPOINT_ADDRESS_PROPERTY,
        "http://new.server.com/some/path/toWs?wsdl");
    ...
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo