Home > A basic, straight-forward tutorial on Spring Web Services

A basic, straight-forward tutorial on Spring Web Services

November 23, 2009 Mike 29 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.

Categories: Tags: , , ,
  1. December 28, 2009 at 14:41 | #1

    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

  2. December 28, 2009 at 14:45 | #1

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

  3. January 27, 2010 at 16:50 | #1

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

    Thanks.

  4. RC
    February 06, 2010 at 14:05 | #1

    Hi,

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

  5. February 06, 2010 at 17:44 | #1

    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.

  6. RC
    February 06, 2010 at 17:44 | #1

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

  7. RC
    February 06, 2010 at 18:18 | #1

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

  8. February 17, 2010 at 02:25 | #1

    same problem... deployed in tomcat but could not generate the wsdl file........Pls help.........

  9. RC
    February 18, 2010 at 00:26 | #1

    Could not see .wsdl earlier since was looking at the wrong path.

  10. RC
    February 18, 2010 at 00:30 | #1

    I have a question. I worked on a spring webservice that works fine on my local tomcat. however, when I deployed the same .war file on a tomcat on different location, it stopped working. I changed the IS_AUTHENTICATED_ANONYMOUSLY to /**, but it didnt work. What is wrong?

  11. February 18, 2010 at 07:33 | #1

    Why don't you post that question on the Spring forums?

  12. March 02, 2010 at 12:26 | #1

    "The dependencies for a successful deployment are roughly"

    What exactly libraries related to JAXB ? If'm not mistaken, JAXB is in javax.xml... and in org.springframework.oxm.jaxb.

    Am I wrong?
    Should I add another additional JAXB libs for Netbeans?

  13. March 02, 2010 at 12:46 | #1

    JAXB is in java.xml... The second choice are Spring's APIs for using JAXB. Just add the JAXB 2.0 "library" in NetBeans.

  14. March 03, 2010 at 08:45 | #1

    Can you tell me for what exactly Castor is used.
    Is it used for marshaling/unmarshaling stuf? or for DAO?

    I Spring + MVC + Hibernate project. And i am forking on wb service with Spring WS.

    Want to clarify all libraries which you listed above.

  15. March 03, 2010 at 09:19 | #1

    According to the great oracle Google, Castor is a XML-mapping library. How it is used here is beyond my pay grade at the moment.

  16. March 04, 2010 at 13:05 | #1

    I've done everything. I have generated wsdl file, and Test is good.


    But I have one trouble - I've researched netbeans project and JAXB, and "Regenerate Java code" doesn't get new information from /web/WEB-INF/schema.xsd after editing it.

    But when I edit src/conf/xml-resources/jaxb/UNIQUENAME/schema.xsd (this file is shown under "JAXB Binding" item in project) - THEN "Regenerate Java code" works, and appropriated package is regenerated.


    So, is it normal?
    I thought that I can edit /web/WEB-INF/schema.xsd and after that regenerate these new changes to Java.

    Please help me to understand how JAXB work on Netbeans with XSD file regeneration.

  17. March 04, 2010 at 13:19 | #1

    The JAXB plugin for NetBeans caches the XSD. If you right click on the JAXB binding part of the project, one of the options should be something like "refresh schema" or reload it. Point is, it'll say something that indicates that it will recache the XSD from the WEB-INF directory.

  18. March 10, 2010 at 02:47 | #1

    I'm trying to deploy it on jboss-5.1.0.GA but getting following error even though all specified jars are in application's lib directory and in JBoss lib folder as well.

    Caused by: java.lang.NoClassDefFoundError: org/springframework/web/servlet/FrameworkServlet
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)....................

  19. March 10, 2010 at 09:50 | #1

    That means you are missing part of the Spring Framework or JBoss is having problems loading or locating the Spring Framework jars.

  20. March 11, 2010 at 05:25 | #1

    Now It is throwing following error

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'marshaller' defined in ServletContext resource [/WEB-INF/spring-ws-servlet.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.util.ObjectUtils.isEmpty([Ljava/lang/Object;)Z
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:341)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:146)

  21. March 11, 2010 at 05:29 | #1

    You are correct.... i was missing some jars. Now i have included but getting another error

  22. March 11, 2010 at 07:33 | #1

    That's a very tricky type of bug in Spring. It means that something is wrong with the way you defined the marshaller bean. Spring can be very anal about bean definitions, so I suggest you focus your attention on carefully reading over how you defined that bean and if you see nothing there, look at any other beans it depends on that you've defined in the Spring context file.

  23. March 11, 2010 at 07:35 | #1

    I don't know what your requirements are, but in my limited experience with web services, if you can avoid using SOAP services altogether in favor of REST, you should. I've found JAX-RS to be significantly easier to use than any SOAP toolkit.

  24. March 12, 2010 at 00:22 | #1

    I've copied all the code from this site. Also i tried the following thing

    Without initializing anything here, i tired to access the service, expecting atleast error message should change. But i got no success. Could you tell me what might be wrong?

  25. March 12, 2010 at 08:13 | #1

    No idea. It worked for me, and judging from the comments, most people have found it to work for them as well. I copied everything straight out of a working NetBeans project, so there's no reason why it shouldn't work in at least a web app project created in NetBeans and deployed on Tomcat.

  26. Jan
    April 28, 2010 at 08:25 | #1

    Hi,

    At this moment I am sort of struggling with Spring WS. Sometimes it works, most of the time it does not (not even the build of downloaded samples).

    I find your tutorial very readable and would like to try to run your code on my machine. Do you happen to have a link to the sources as a simple bundle?

    thanks,
    Jan

  27. May 05, 2010 at 15:17 | #1

    Unfortunately, I don't. However, if you copy and paste those code samples as-is and add the jar files referenced above, it should be build without incident for you. I got those jar files from the Maven build of the Spring-WS samples, IIRC.

  28. Gil
    May 20, 2010 at 12:30 | #1

    Hi, how did you get the 404 fixed. I am getting as well and I have tried using SOAPUI as a test and it too does not find it. There is no WSDL for this as it is automatically generated. I renamed the URL to match my local machine (localhost and port) but to no avail.

    Any help is appreciated.

  29. August 31, 2010 at 00:57 | #1

    I used the xjc.bat to generate Marshalling and Unmarshalling java files. (jaxb-ri-20100511)
    In the class BasicMarshallingEndPoint I am getting the error "Cannot instantiate the type ResponseObject" at line ResponseObject response = new ResponseObject();
    Please help.