Spring Web Services

Spring Web Services: Creating SOAP Web service using the most popular Spring framework, A step by step guide is explained here for Contract-first SOAP web service development using spring web service.


1.       Initial Setup Can be done as following.
Install JDK (the version that I have used here is 1.6.0_45)
Download and extract the following into C drive (or any other location which you feel is more appropriate.)
  • Apache Maven (version used for the purpose of this guide is 3.1.1)
  • Apache tomcat (version used for the purpose of this guide is 7.0.42
  • JAXB (version used for the purpose of this guide is jaxb-ri-2.2.7). Add C:\ jaxb-ri-2.2.7\bin to the system path
Eclipse IDE (or any other IDE which you are comfortable with)

2.       Create a project structure using the following maven command (if you are running the maven command for the first time then it will take a while to create the project structure because all the required dependencies has to be downloaded first)

mvn archetype:generate -DarchetypeGroupId=org.springframework.ws -DarchetypeArtifactId=spring-ws-archetype -DarchetypeVersion=2.1.2.RELEASE -DartifactId=bookstore-web-service -DgroupId=com.bookstore -DinteractiveMode=false

Following directory structure will be created. Also web.xml and spring-ws-servlet.xml are created by default in WEB-INF directory






3.       Create a new directory by name java under bookstore-web-service/src/main directory.



4.       Create eclipse project files by running the following maven command. This command has to be executed from inside bookstore-web-service directory where pom.xml is located
mvn eclipse:eclipse

This command will create .settings folder, target folder, .classpath file and .project file

5.       Create data contract using xml schema by name bookstore.xsd inside WEB-INF directory. This data contract defines the message that the web service will accept.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:bs="http://www.bookstore.com/schema"
           elementFormDefault="qualified"
           targetNamespace="http://www.bookstore.com/schema">

    <xs:element name="Book">
           <xs:complexType>
               <xs:sequence>
                   <xs:element name="name" type="xs:string"/>
                   <xs:element name="author" type="xs:string"/>
                   <xs:element name="price" type="xs:string"/>
               </xs:sequence>
           </xs:complexType>
    </xs:element>
   
    <xs:element name="GenericResponse">
       <xs:complexType>
                     <xs:sequence>
                           <xs:element name="code" type="xs:int" />
                           <xs:element name="message" type="xs:string" />
                     </xs:sequence>             
       </xs:complexType>
    </xs:element>

       <xs:element name="AddBookRequest">
              <xs:complexType>
                     <xs:sequence>
                           <xs:element ref="bs:Book"/>
                     </xs:sequence>
              </xs:complexType>
       </xs:element>
      
       <xs:element name="AddBookResponse">
              <xs:complexType>
                     <xs:sequence>
                           <xs:element ref="bs:GenericResponse"/>
                     </xs:sequence>
              </xs:complexType>
       </xs:element>

       <xs:element name="GetBookRequest">
              <xs:complexType>
                     <xs:sequence>
                           <xs:element name="name" type="xs:string"/>
                     </xs:sequence>
              </xs:complexType>
       </xs:element>

       <xs:element name="GetBookResponse">
              <xs:complexType>
                     <xs:sequence>
                           <xs:element ref="bs:Book"/>
                     </xs:sequence>
              </xs:complexType>
       </xs:element>
      
       <xs:element name="DeleteBookRequest">
              <xs:complexType>
                     <xs:sequence>
                           <xs:element name="name" type="xs:string"/>
                     </xs:sequence>
              </xs:complexType>
       </xs:element>
      
       <xs:element name="DeleteBookResponse">
              <xs:complexType>
                     <xs:sequence>
                           <xs:element ref="bs:GenericResponse"/>
                     </xs:sequence>
              </xs:complexType>
       </xs:element>
</xs:schema>




No comments :

Post a Comment