How do I specify host and port when accessing a we

2019-02-27 02:37发布

问题:

I have a WSDL file for a web service. I'm using JAX-WS/wsimport to generate a client interface to the web service. I don't know ahead of time the host that the web service will be running on, and I can almost guarantee it won't be http://localhost:8080. How to I specify the host URL at runtime, e.g. from a command-line argument?

The generated constructor MyService(URL wsdlLocation, QName serviceName) doesn't seem like what I want, but maybe it is? Perhaps one of the variants of Service.getPort(...)?

Thanks!

回答1:

The constructor should work fine for your needs, when you create MyService, pass it the url of the WSDL you want i.e. http://someurl:someport/service?wsdl.



回答2:

If you have a look in the generated source close to the generated constructor, you should be able to figure out what to put in it from the default constructor, should look something like:

public OrdersService() {
    super(WSDL_LOCATION, new QName("http://namespace.org/order/v1", "OrdersService"));
}

You should be able to find the def of WSDL_LOCATION in the static field further up in the class.



回答3:

In your generated code (eg: say "HelloWorldWebServiceImplService" ) look in to the static block on the top which will have reference to the WSDL url or wsdl file which is under META-INF.

 /*
static {
    URL url = null;
    try {
        url = new URL("http://loclahost/HelloWorld/HelloWorldWebServiceImpl?wsdl");
    } catch (MalformedURLException e) {
        java.util.logging.Logger.getLogger(HelloWorldWebServiceImplService.class.getName())
            .log(java.util.logging.Level.INFO, 
                 "Can not initialize the default wsdl from {0}", "http://loclahost/HelloWorld/HelloWorldWebServiceImpl?wsdl");
    }
    WSDL_LOCATION = url;
}
*/

Once you comment this you also need to comment out the default construtor and needless to say intialize the static WSDL_LOCATION = null; (to null) So you will not have two constructors as shown below.

public final static URL WSDL_LOCATION = null;
public HelloWorldWebServiceImplService(URL wsdlLocation) {
    super(wsdlLocation, SERVICE);
}

public HelloWorldWebServiceImplService(URL wsdlLocation, QName serviceName) {
    super(wsdlLocation, serviceName);
}

Calling Webservice : Now in the client call where you instantiate this object Pass the webservice URL as an argument as shown

   //You can read mywebserviceURL from property file as String.
    String mywebserviceURL = "http://myqamachine.com/HelloWorld/HelloWorldWebServiceImpl?wsdl"
    URL WsURL = new URL(mywebserviceURL);
    HelloWorldWebServiceImplService webService = new HelloWorldWebServiceImplService(WsURL);

So here you can point the webservice url dynamically.