----

Securing Spring Web Services : Chapter 2

Creating XML Schema and WSDL for Web Service


1.       Now we will create a xml schema which will define the request and response like       AddBookRequest and AddBookResponse for our web service. This is called data contract. Create xml schema by name bookstore.xsd inside WEB-INF directory. The following 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="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 name="message" type="xs:string"/>
                     </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 name="message" type="xs:string"/>
                     </xs:sequence>
              </xs:complexType>
       </xs:element>
</xs:schema>

2.       Add dynamic-wsdl configuration to spring-ws-servlet.xml. The <dynamic-wsdl> builds a WSDL from a XSD schema so that you don’t have to create a wsdl by hand.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:sws="http://www.springframework.org/schema/web-services"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/web-services
       http://www.springframework.org/schema/web-services/web-services-2.0.xsd">
    <sws:annotation-driven/>
    <sws:dynamic-wsdl id="bookstore" portTypeName="BookStoreInterface"
              locationUri="http://localhost:8080/bookstore-web-service/services"
              targetNamespace="http://www.bookstore.com/schema">
       <sws:xsd location="WEB-INF/bookstore.xsd"/>
    </sws:dynamic-wsdl>
</beans>

Please note here that the id “bookstore” will be used to access wsdl by the clients.

3.       Build the project using the following maven command

mvn clean package

This output of this command will bookstore-web-service.war created inside bookstore-web-service\target directory

4.     Deploy bookstore-web-service.war by copying it in C:\ apache-tomcat-7.0.42\webapps directory. Start tomcat by running startup.bat located in C:\ apache-tomcat-7.0.42\bin directory.

5.    Access wsdl from the browser using the following url
      wsdl will be displayed in the browser indicating that the bookstore web service is published and is         now available for client/consumer. However there are few more steps to be done before the client           could invoke bookstore web services.

No comments :

Post a Comment