How do I specify host and port when accessing a web service from JAX-WS-generated code?
Asked Answered
S

3

3

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!

Sarah answered 16/3, 2009 at 2:1 Comment(0)
C
2

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.

Counterfeit answered 16/3, 2009 at 7:12 Comment(3)
Ok. Just so I understand... MyService will request the WSDL and then use whatever's in the <soap:address> tag to determine where the service actually is?Sarah
I think that is right, I've tried to look it up but I can't find anything to answer clearly.Counterfeit
Yes. JAX-WS will load the WSDL at wsdlLocation and then parse the WSDL for the service and port specified by serviceName and portName, respectively. JAX-WS will use the URL specified in the WSDL port when invoking the Web service. Thus, you will need to know the WSDL URL, service QName, and port QName in order to interact with the remote service using the code you listed above.Conner
C
1

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.

Chamberlain answered 19/8, 2010 at 9:50 Comment(0)
F
1

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.

Fenwick answered 28/1, 2014 at 22:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.