Simple Web Service Using Spring

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 :

pom.xml

 

  • Design Schema:

The schema for representing the request and response will be  :

The schema represents two elements : MathServiceRequest and MathServiceResponse  defined in namespace http://codereq.com/mathservice/schemas

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 MathServiceRequest.java and MathServiceResponse.java. Apart from these classes, ObjectFactory.java and package-info.java will also be created.

MathServiceRequest.java :

MathServiceResponse.java

  • 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

web.xml

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 ” http://codereq.com/mathservice/schemas ” will be handled by this method.

MathServiceEndpoint.java

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.

MathService.java

 

  • 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.

18 thoughts on “Simple Web Service Using Spring

      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 MathService.java – (for me its not created by xjc tool even i followed all your steps given here)

        Reply
      2. mhotchkiss7291@gmail.com

        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 MathService.java 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.

        Reply
  1. Pingback: spring ws |

  2. Shubham

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

    jdom
    jdom
    2.0

    it should be

    org.jdom
    jdom
    2.0

    It was giving maven compile error with “org”.

    Reply
  3. Shubham

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

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

    Reply
  4. vinod verma

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

    Thanks

    Reply
  5. 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.

    Reply
    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.

      Thanks.

      Reply

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">