WebServices client using Spring and Axis

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 http://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
    
    
          http://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 http://api.google.com.

Have fun searching the web!

Comments (1)

  1. Anderson - Reply

    January 26, 2014 at 3:55 am

    Simply desire to say your article is as amazing. The clearness in your post is just great and i can assume you're an expert on this subject. Well with your permission allow me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please keep up the gratifying work.

Add a Comment