A basic, straight-forward tutorial on Spring Web Services

| 7 Comments

Introduction

The Spring Web Service tutorial is pretty good, but doesn't really go down far enough to explain how to build a basic Spring Web Service from scratch. People who are already familiar with Java and either NetBeans or Eclipse won't need much more than the listings under the source code and configuration files section. For the sake of brevity, here is a simple explanation of what each file is and where it should go in a NetBeans project.

  1. Web.xml - Standard Java Enterprise Edition web configuration file. Goes in web/WEB-INF in a NetBeans project.
  2. schema.xsd - A XML schema which defines two XML elements: RequestObject and ResponseObject. Goes in web/WEB-INF.
  3. spring-ws-servlet.xml - A Spring context file which defines a number of Java beans either from the Spring Web Service toolkit or from the files added/generated in the NetBeans project. Goes in web/WEB-INF. This context does the following things that are worth-noting:
    • Defines an instance of the endpoint object
    • Provides a "payload mapping" which maps the namespace-qualified RequestObject XML document to the endpoint.
    • Provides a JAXB2 Marshaller which is used by the endpoint.
    • Adds a validating interceptor object which validates the incoming and outgoing XML against the schema.
    • Defines an instance of DefaultWsdl11Definition which will generate a WSDL on the fly at the absolute URL of the deployed web application under the file name basicSpringWS.wsdl.
  4. org.springframework.ws.examples.BasicMarshallingEndpoint - A POJO which extends the AbstractMarshallingPayloadEndpoint base class from the Spring Web Services toolkit. The incoming object will be a JAXB2-generated RequestObject. The return should be a JAXB2-generated ResponseObject. In NetBeans, simply add the contents of figure 4 to a new POJO with the same package and class name.
  5. com.springframework.ws.examples.test.BasicTest - a JUnit4 test case which extends off of WebServiceGatewaySupport, a Spring helper class which makes sending to and receiving from a web service as simple as defining a source and a result object.

The dependencies for a successful deployment are roughly:

  • activation-1.1.1.jar
  • aopalliance-1.0.jar
  • castor-1.2.jar
  • commons-logging.jar
  • log4j-1.2.15.jar
  • saaj-api-1.3.jar
  • saaj-impl-1.3.2.jar
  • spring-aop-2.5.6.jar
  • spring-beans-2.5.6.jar
  • spring-context-2.5.6.jar
  • spring-context-support-2.5.6.jar
  • spring-core-2.5.6.jar
  • spring-oxm-1.5.8.jar
  • spring-oxm-tiger-1.5.8.jar
  • spring-web-2.5.6.jar
  • spring-webmvc-2.5.6.jar
  • spring-ws-core-1.5.8.jar
  • spring-ws-core-tiger-1.5.8.jar
  • spring-xml-1.5.8.jar
  • stax-api-1.0-2.jar
  • wsdl4j-1.6.1.jar
  • xalan-2.7.0.jar
  • xercesImpl-2.8.1.jar
  • xml-apis-1.3.04.jar

That list is just one that I threw together to be on the safe side. It's based on the files that were added by maven when I ran the maven package target on the EchoWS example, and trial-and-error getting the rest of the dependencies resolved.

Source Code and Configuration Files

Figure 1: web.xml


Figure 2: schema.xsd


Figure 3: spring-ws-servlet.xml


Figure 4: Endpoint


Figure 5: JUnit Test


Steps in NetBeans 6.5+


Create a new web project.

Spring WS Basic Tutorial: New Project Part 1 Spring WS Basic Tutorial: New Project Part 2

Spring WS Basic Tutorial: New Project Part 3

Add the Spring and Spring-WS libraries and dependencies to the project (both the regular project and the test libraries settings) by right clicking on the project and selecting Properties from the popup menu.

Spring WS Basic Tutorial: Libraries


Open the WEB.xml file in WEB-INF (under Web Pages in the Project tree), and copy in the contents of Figure 1. Right click on the project, and add two new empty files into the WEB-INF directory and name them schema.xsd and spring-ws-servlet.xml. Paste in the contents of figures 2 and 3 respectively. Now, add a new Java class to the project, using the contents of figure 4.

Spring WS Basic Tutorial: Endpoint Creation


Create a JUnit test using the contents of figure 5. Save everything, right click on the project and select deploy. That will build the war file, start Tomcat or Glassfish, and install the web service. Once the service is deployed, right click on the JUnit and run it to test it.

If I missed a step that needs clarification, just leave a comment in the comments section.

7 Comments

What about the RequestObject and ResponseObject classes?
My compiler cries about them that they cannot be resolved to a type inside the BasicMarshallingEndpoint class
Thank you

Did you create them through the JAXB compiler/NetBeans task?

I finally got my first spring webservice working because of your example.

Thanks.

Hi,

Can you please give an example of how to call this service using javascript as a client

You really wouldn't want to call a SOAP web service via JavaScript. If you need to be able to easily call a web service from JavaScript, implement it as a REST service.

could not make it work...
got the error:
org.springframework.ws.client.WebServiceTransportException: Not Found [404]

It works now!! Redeployed the war file on tomcat. However, could not see the .wsdl

Leave a comment