Blog

WebServices client using Spring and Axis

12 Jan, 2006
Xebia Background Header Wave

Using Spring and Axis it is very easy to create a client for a Webservice by just having access to a WSDL file. I’ve implemented a solution for this at one of our customers. Based upon this I’ve created a sample using the Google Web-API.

In this sample we will generate our own client jar using Axis, and use Spring to inject a Proxy to the WebService into our application. Notice that we do not use the SDK that is provided by Google for their Web-API. This SDK however provides a nice starting point to do any further development with the Google Web-API, so it is worth browsing through.
First of all, the WSDL file can be found at https://api.google.com/GoogleSearch.wsdl, this is the basis for our new client. Using this file we can create a client jar file using Axis’ WSDL2Java. Though this technically isn’t needed, it gives us a head-start. It is perfectly possible to code both the Java interface to the webservice and its return objects by hand. The client jar can be generated using the following ant-tasks.


   
 
 
   
     
   
 
 
   
     
   
 
 
   
     
     
       
     
   
 

  

Using these ant-tasks a new jar is created called google-ws.jar. This jar can be included in the classpath of your application. But instead of using the GoogleServiceLocator and other plumbing classes that are generated, we will have the WebService injected in a client service in our application by Spring. In Spring we do this by adding the following bean definition to our applicationContext.xml file.

  
    
          com.google.search.webservice.GoogleSearchPort
    
    
          https://api.google.com/GoogleSearch.wsdl
    
    
          urn:GoogleSearch
    
    
          GoogleSearchService
    
    
          GoogleSearchPort
     
 

This bean definition generates a new dynamic proxy implementing the specified “serviceInterface” (in this case from our client jar) which can be injected into your own beans in your application. This proxy takes care of all the plumbing and SOAP calls. Notice which parts of the WSDL file map onto which properties in the bean-definition. To use the Google Web-API you need to request a license-key from Google. You can find the procedure to request one at https://api.google.com.
Have fun searching the web!

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts