Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure webservice URL for client from properties file with Netbeans 7 and Axis2

I'm new to webservice development. I'm using Netbeans 7.0 with the Axis2 plugin and Tomcat 7.

I created one project for the server components where I define the web methods, and then created another project for the client components. The client was created in Netbeans by selecting New -> Webservice Client.

When you select New -> Webservice Client in Netbeans, it asks you right then for a WSDL URL. So of course I gave it the WSDL URL from my local Tomcat installation. But what about when I distribute this as a real application? The users aren't going to point their clients at http://localhost:8080/axis2/services/?wsdl. I mean, when running the client from the IDE, it all works fine, but when I distribute this (it's a labor management application by the way where you clock in / out at one or more clients and time cards are written to a central DB), each client needs to be able to point at the webservice URL of whatever production server it's supposed to connect to.

I'd like to store the webservice URL in a properties file, but don't really know what all to do programmatically at the client to make the call to the URL that's loaded from the properties file.

In my client's dist folder, if I open the JAR that netbeans created with WinZip, I see a file name jax-ws-catalog.xml that has the URL in it (which is pointed at localhost). I assume this is where the URL that's used at runtime comes from.

So what's the correct way to go about this? I've searched around, but the things I've found looking on google searches tend to show webservice calls being made in a completely different way than the auto-generated code that Netbeans puts together, and I'd like some info specific to how Netbeans creates a webservice client so that I don't end up making changes just to have the IDE overwrite them.

Thanks! Sorry for the long explanation.

-Jim

like image 701
Jim Avatar asked Mar 06 '12 20:03

Jim


2 Answers

I actually figured this out in a different way, and it's probably kind of specific to the way Netbeans does things. The answer Shott85 provided is a good one also, but I think this is more specific to the way Netbeans' auto-generates code.

So I have a project where all the web methods reside named SimplyLaborServer, and a project that has the webservice client named SimplyLaborClient.

In Netbeans, under the SimplyLaborClient project in the "Generated Sources (jax-ws)" node, they have a SimplyLaborServer.java file that has a class which extends Service. It has a private URL that is hard coded to my local server's URL as follows...

url = new URL("http://localhost:8080/axis2/services/SimplyLaborServer?wsdl");

And in the default constructor, it uses this URL. But it also provides a constructor as follows where I can specify the URL...

public SimplyLaborServer(URL wsdlLocation) {
    super(wsdlLocation, SIMPLYLABORSERVER_QNAME);
}

So when I have an auto-generated method that looks like this in my client...

private static String testConnection() {
    simplylaborclient.SimplyLaborServer service = new simplylaborclient.SimplyLaborServer();
    simplylaborclient.SimplyLaborServerPortType port = service.getSimplyLaborServerHttpSoap12Endpoint();
    return port.testConnection();
}

I can simply load a Properties object that has the endpoint URL and change the one line to something like below, where props is a Properties object that has endpointUrl defined with the correct URL.

simplylaborclient.SimplyLaborServer service = new simplylaborclient.SimplyLaborServer(new URL(props.getProperty("endpointUrl")));

My only concern is that these methods are kind of auto-generated when you drag and drop them from the "Web service references" node. I don't want them to be overwritten if I make additional changes server-side.

So I'm still open to feedback if this is the correct thing to do here or not.

Thanks

like image 168
Jim Avatar answered Sep 30 '22 08:09

Jim


This has been answered before: How to change webservice url endpoint?

NetBeans uses plain JAX-WS to generate client code, so the answer above should work for you. You just need to add some code to get the endpoint URL from a properties file.

like image 38
schottsfired Avatar answered Sep 30 '22 08:09

schottsfired