Droy
Droy

Reputation: 183

wsdl file location path setup in java

I'm using wsdl files to generate client & is working fine on my local server since the generated files have path like "C:\path\to\wsdl\Air.wsdl"

I'm trying to replace all the paths as above to the one that work on live server I've found some hints but I'm not so sure how to implement it (I'm new to web services).

Below is how my files currently look like

@WebServiceClient(name = "AirService", 
              wsdlLocation = "file:/C:/Eclipse WorkSpace/TravelPortCXF/WebContent/wsdl/air_v33_0/Air.wsdl",
              targetNamespace = "http://www.travelport.com/service/air_v33_0") 
    static {
    URL url = null;
    try {
        url = new URL("file:/C:/Eclipse WorkSpace/TravelPortCXF/WebContent/wsdl/air_v33_0/Air.wsdl");
    } catch (MalformedURLException e) {
        java.util.logging.Logger.getLogger(AirService.class.getName())
            .log(java.util.logging.Level.INFO, 
                 "Can not initialize the default wsdl from {0}", "file:/C:/Eclipse WorkSpace/TravelPortCXF/WebContent/wsdl/air_v33_0/Air.wsdl");
    }
    WSDL_LOCATION = url;
} 

This seems to be like it should be changed (I'm not sure about that, found it on googling)

@WebServiceClient(name = "AirService", 
              wsdlLocation = "classpath:wsdl/air_v33_0/Air.wsdl",
              targetNamespace = "http://www.travelport.com/service/air_v33_0") 

              static {
    URL url = AirService.class.getClassLoader().getResource("wsdl/air_v33_0/Air.wsdl");
    if (url == null) {
        java.util.logging.Logger.getLogger(AirService.class.getName())
            .log(java.util.logging.Level.INFO, 
                 "Can not initialize the default wsdl from {0}", "classpath:wsdl/air_v33_0/Air.wsdl");
    }       
    WSDL_LOCATION = url;   
}

But I actually don't understand how that method works.. Can someone please make me understand how can I change the path and where to place the wsdl files in the project so that it works properly on the live server and also the difference between both the codes.

Also I don't want to generate every-time when build is created, I want to generate the files (that are already been generated), place the wsdl somewhere in the project folder from where it can be accessed and then change the path in the Java files (by replacing all the paths at once) as in the above codes.

Upvotes: 1

Views: 5341

Answers (1)

Marek Gregor
Marek Gregor

Reputation: 3851

Few intro points for you:

Wsdl file contains description webmethods callable on the server (in general method names, their input parameters and its types and types of results of this methods). Therefore wsdl file is like contract between client and server - client knows what methods to call on the server, what parameters it will need to send with method name, and what type of result it can expect.

You can download and store wsdl file from server, and then use it for generation of special client class(es) (called stubs). This classes have java methods 1 to 1 corresponding to web service methods described in wsdl file. Source code of these classes is generated automatically by special tool (wsimport) which reads content of wsdl file and generate corresponding methods. From your perspective as developer of client, it is then very simple - you need only in some way create instance of this class (giving it url of wsdl file on the server) and call it's method like you call any other Java class method. (Generated implementation code of this methods cares for serialization of inputs and sending of webservice request to server, and then deserialization of response back to you, so you will obtain normal Java object as a result of call).

Maybe you now ask, why you need to set url to server's wsdl file before call of webservice method. Answer is that client code before calling your webservice automatically download's wsdl from the server and check's if it does not change meantime (if yes it does not call webservice and throws error). It sometimes happen that developer of server changed e.g. parameters of webservice method and you as client developer have still old version of wsdl, therefore validation aspect of this technology saves you a lot of time.

Now real examples: Look at JAX-WS deployment best practice: WSDL location and client generation question to see client code how webservice method (hello) is called using HelloWorldPOCImpl stub class.

For server side implementation you need only two annotation @Webservice and @WebMethod, please look at chapter 3.1 to see example: https://metro.java.net/getting-started/building-a-simple-metro-application.html For server, you dont need to create wsdl file manually, server generates wsdl file automatically - based on your code and mentioned annotations.

Upvotes: 1

Related Questions