Simple Web Service Using Spring

By | November 18, 2012

Creating Web Service Using Spring

This article will show how to create a simple web service using spring provided infrastructure.  We will also see how the dependency injection, inversion control mechanism and other features of spring can be used in web service applications.

High level steps to create our web service using spring are :

  • Use Case:

We will design a simple web service using spring which will tell if the given number if even or not.

  • Create a maven webapp project:

Change to your project directory and create a maven webapp project using the following command.

On executing this command , a project directory springws-one will be created.

We will call this as out SWS_DIR

  • Maven POM file :



  • Design Schema:

The schema for representing the request and response will be  :

The schema represents two elements : MathServiceRequest and MathServiceResponse  defined in namespace

Copy the schema to <<sws_dir>>/src/main/resources folder.

  • Convert Schema to Java class:

Use the xjc tool to convert the schemas to java classes.

On executing this command, the java classes corresponding to schemas will be created. They will be and Apart from these classes, and will also be created. :

  • Configure the web.xml

Since the spring web service is a web based application, we need to configure the web.xml in a way that any incoming request will be mapped to spring specific servlet. This servlet will be responsible to identify our endpoint implementations and delegate the request to them.

The location on web.xml is : <sws_dir>/src/main/webapp/WEB-INF


The web.xml is configured such that all the incoming requests will be handled by MessageDispathcherServlet class. This is spring specific class responsible to map the incoming requests to configured Endpoint annotated classes.


  • Develop the sever side implementation :

The endpoint implementation class is annotated with @Endpoint  which will enable the spring’s MessageDispatcherServlet to direct the incoming requests to this class.

The method which will handle the web service request will be annotated with @PayloadRoot. The @PayloadRoot will have parameters which will indicate what payload will be handled by the method. It has the following format :

The attributes tell that any incoming request xml which has MathServiceRequest element in namespace ” ” will be handled by this method.

The primary method in this class is the handleMathServiceRequest which is annotated with @PayloadRoot. The parameter are annotated with @RequestPayload which tells that the parameter should be bound to request payload. Similarly, the return type MathServiceResponse is annotated with @ResponePayload which will bind the response with response payload.


  • Spring Configuration File

The spring configuration file is named spring-ws-servlet.xml. The name is derived from name of servlet and appended with servlet. <<servlet_name>>-servlet.xml. The file should be located at  <<sws_dir>/src/main/webapp/WEB-INF/:

Take note of sws:dynamic-wdl element. This is element is a spring configuration which will generate the wsdl on fly . Once the application is deployed, the url to access the wsdl is : http://localhost:8080/springws-one/math.wsdl .

  • Compile  and deploy.

Compile the application using mvn clean install from SWS_DIR . Once the war is generated in the target folder, copy the war to servlet container and deploy.

21 thoughts on “Simple Web Service Using Spring

  1. abdel

    think you for this tuto ,can you give me the code of class MathService

      1. govind

        In the place of servlet configuration you have given wsdl. Is this correct ? Could you please give valid servlet configuration file and implentation of – (for me its not created by xjc tool even i followed all your steps given here)

        1. Jim Bradford

          Did the author ever give the code for “”???


        You say that the MathService class will be generated by the xjc tool.

        As a previous reader cited, it does not. At least it does not generate a file if that is what it is supposed to do.

        This missing piece makes the example incomplete for the reader. Please review your steps so that there won’t be yet another of many of incomplete Spring WS examples. All of them work to about 85% and then something is missing just like this one.

  2. Pingback: spring ws |

  3. Arun

    Good one , but not complete , You have not shown how the springws-servlet.xml file looks like. Thanks

  4. Shubham

    One correction to above pom.xml
    According to me, instead of


    it should be


    It was giving maven compile error with “org”.

    1. admin

      Hi Shubham,
      Thanks for this correction. I have made the changes in the pom configuration file.

  5. Shubham

    MathService class is not getting created. Is this tutorial incomplete?

    God! After investing so much of time, I am seeing a incomplete tutorial !! :(

  6. vinod verma

    Can you please let us know about file because without this java class your tutorial is seems uncompleted.


    1. Santiago

      I did the deploy succesfully but i need a URL for testing the soap ws. Could u explain how?
      Or which path to follow to view the wsdl.

  7. Gaur

    Hey..Thank you very much for this tutorial.

    Could you please post complete example? here is neither web.xml nor spring-ws-config.xml

    Please post complete code … it will help greatly……..

    I am confused in creating wsdl file. How to create it …. there are many tutorials on net but all are confusing more…. they have implemented in various ways…………….. Please post your all code here in this example..

    Thank you soo much in advance.

    1. admin

      Hi Gaur,

      I have made the changes to this article and have added the MathService file. I have also made changes to the spring servlet configuration file and included configuration to generate the wsdl dynamically.


      1. Kevin

        I still could not see any MathService file get created. Please advise/revise.
        I also get error on generating java classes from xsd. I need to change xs:scheme to xsd:scheme

        else will get error
        because 1) could not find the document; 2) the document could not be
        read; 3) the root element of the document is not .
        unknown location

        Failed to parse a schema.

  8. Savani

    Could you please provide the complete source code link ? Please help !


Leave a Reply

Your email address will not be published. Required fields are marked *