Thursday 3 December 2009

Getting Started With Spring Web Services

Being a newbie in the world of web services I was asked to set one up on my current project. As we are using Spring MVC & STS it made sense to continue down the Spring route.

Also Spring-WS supports contract first whereby one starts with the WSDL first and then implements the endpoint using Java as opposed to contract last where you start with the code first and generate the WSDL from that. There are many advantages outlined in the Spring Tutorial extensively, but one of the biggest selling points for me was that in a contract first the contract would remain constant over time whereas in a contract last the contract might be subject to change as the Java code changes.

The Spring Tutorial above will take you through a detailed outline of how to set up a Spring Web Service, in my post (from my now newly acquired Spring Web Service wisdom) I will take you through the basic set up for a web service with the generation of a WSDL. This simplification is aimed at the newbie for whom it may be a daunting task to set up a Spring web service with very little prior experience. Before I get going, I'd like to thanks Russel Hart (@rus_hart on twitter)  for all his help and guidance with my task.

  • Step 1: Download and Install STS from SpringSource
  • Step 2:  Create a new Maven Project ensuring that the archetype is a webapp.
  • Step 3: Add all the Maven dependencies to your pom.xml.
  • Step 4: Create/Import the XSD that you will be using for the project (this is the data contract).
  • Step 5: Modify the web.xml file (this defines a Spring-WS MessageDispatcherServlet and maps all incoming requests to this servlet).
  • Step 6: Create a ws-servlet.xml file (this file will contain your EndPoints bean and WSDL (service contract) Definitions to autogenerate the WSDL).
  • Step 7: In src--> main create a folder called 'java' and ensure that it is part of the Java buildpath (right click on the project properties --> Java Build Path --> Add Folder and add the java folder to the buildpath).
  • Step 8: In the Java folder create the EndPoint java class.
  • Step 9: Extend the AbstractJDomPayLoadEndPoint class and implement the invokeInternal method. NB: It is possible to extend other classes also most suited to your particular project.
  • Step 10: Ensure that your project builds
  • Step 11: Ensure that you have added the web project to your Tomcat server.
  • Step 12: Run the project on your Tomcat server.
  • Step 13: Check your localhost to ensure your WSDL has been generated (in my instance the URL would be: http://localhost:8080/wsTest/Sample.wsdl).

That is pretty much the it in terms of the steps needed to setup Spring WS and create the service contract. You will then need to write the Java code to implement you Endpoint for your project accordingly.

My Sample project and the WS Tutorial project (from SpringSource) can be found here.

No comments:

Post a Comment

Search This Blog