• Home
  • RSS Feed
  • Log in

WebServices client using Spring and Axis
Posted by Jeroen van Erp terribly early in the morning: January 12th, 2006

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.

 
<path id="base.classpath" description="The base classpath for this build file">
   <fileset dir="lib"><include name="*.jar" /></fileset>
 </path>
 <taskdef name="axis-wsdl2java"
    classname="org.apache.axis.tools.ant.wsdl.Wsdl2javaAntTask">
   <classpath>
<path refid="base.classpath" />
   </classpath>
 </taskdef>
 <target name="wsdlGoogle" description="Initializes the build.">
   <axis-wsdl2java output="build" verbose="true"
                          url="http://api.google.com/GoogleSearch.wsdl">
<mapping namespace="urn:GoogleSearch"
                              package="com.google.search.webservice" />
   </axis-wsdl2java>
 </target>
 <target name="compileGoogle" depends="wsdlGoogle" description="Compile">
   <javac srcdir="build" destdir="build/classes" source="1.5">
     <compilerarg value="-Xlint:-unchecked" />
     <classpath>
<path refid="base.classpath" />
     </classpath>
   </javac>
 </target>
 
<target name="jarGoogle" depends="compileGoogle"
                                    description="JAR up Google webservice">
  <jar destfile="lib/google-ws.jar" basedir="build/classes"/>
</target>
 

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.

 
  <bean id="googleWebService"
           class="org.springframework.remoting.jaxrpc.JaxRpcPortProxyFactoryBean">
<property name="serviceInterface">
          <value>com.google.search.webservice.GoogleSearchPort</value>
    </property>
<property name="wsdlDocumentUrl">
          <value>http://api.google.com/GoogleSearch.wsdl</value>
    </property>
<property name="namespaceUri">
          <value>urn:GoogleSearch</value>
    </property>
<property name="serviceName">
          <value>GoogleSearchService</value>
    </property>
<property name="portName">
          <value>GoogleSearchPort</value>
     </property>
 </bean>
 

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!

  • Share/Bookmark

Filed under Java | No Comments »



No Responses to “WebServices client using Spring and Axis”



Leave a Reply

Click here to cancel reply.

Deployment automation for Java application running on Websphere, WebLogic and JBoss

Archives

  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009

Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India

Categories

  • Java (282)
  • Agile (109)
  • General (50)
  • Testing (42)
  • Performance (42)
  • Hibernate (36)
  • Scrum (33)
  • Podcast (31)
  • Architecture (31)
  • Spring (28)
  • SOA (24)
  • Maven (22)
  • Project Management (22)
  • Middleware (23)
    • Deployment (14)
  • Flex (17)
  • JPA (17)
  • Eclipse (15)
  • Xebia Labs (15)
  • Quality Assurance (14)

Tag Cloud

    Lean Testing Java Scala Hibernate IntelliJ Spring SOA XML Scrum qcon Ajax Xebia Seam product owner Functional Programming Architecture Poppendieck Agile esb Agile Awareness Workshop Grails Semantic Web Groovy Closures fitnesse Maven Performance JavaOne Introduction to Agile