overriding or setting web service endpoint at runtime for code generated with wsimport

前端 未结 2 550
时光说笑
时光说笑 2020-11-30 21:21

Using code that was generated with wsimport, can the service endpoint be overridden without having to regenerate the code?

I have written a simple java

相关标签:
2条回答
  • 2020-11-30 21:50

    Your client can set the end-point in the service "port" at runtime via the BindingProvider interface.

    Consider the JAX-WS client in this JAX-WS tutorial. Another way to write this code would be:

    HelloService service = new HelloService();
    Hello port = service.getHelloPort();
    BindingProvider bindingProvider = (BindingProvider) port;
    bindingProvider.getRequestContext().put(
          BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
          "http://foo:8086/HelloWhatever");
    String response = port.sayHello(name);
    

    Caveat: I haven't downloaded the tutorial code and tested this code against it.

    0 讨论(0)
  • 2020-11-30 22:07

    I faced the same issue, and it was terrible coz once the code is moved to production it always looked for the hardcoded WSDL location i.e. Windows C:........etc

    I have gone thru various post and pages to find the answer however all was failing then found myself a way by looking at the Service Class generated by JAX-WS imports.

    I had to override the JAX-WS WSDL location implementation in my calling class like this.

    URL baseUrl;
    URL wsdlURL = null;
    baseUrl = <your Services>.class.getResource(".");
    try {
        wsdlURL = new URL(baseUrl, "http://<your path>?wsdl");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
    
    <your Services> yourServices = new <your Services(wsdlURL,new QName("your namespace", "<your service name>"));
    System.out.println(Services.getWSDLDocumentLocation());
    YourInterface YourInterfacePort =  yourServices.getServicePort();
    BindingProvider bindingProvider = (BindingProvider)YourInterfacePort;
    bindingProvider.getRequestContext().put(
              BindingProvider.ENDPOINT_ADDRESS_PROPERTY,      url);
    

    YourInterfacePort.methods();

    0 讨论(0)
提交回复
热议问题