----

Creating Web Service Cont..

6.       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">

<!-- detects @PayloadRoot -->
    <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 and all the client request will be handled by the MessageDispatcherServlet configured in web.xml

7.       Build the project using the following maven command from bookstore-web-service directory

mvn clean package

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

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

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

10.       Bind the contract defined by bookstore.xsd to java classes using JAXB’s xjc command at command prompt. Run the xjc command from bookstore-web-service\src\main directory

xjc –d java webapp\WEB-INF\bookstore.xsd

This command will result in creation of following java classes

com\bookstore\schema\AddBookRequest.java
com\bookstore\schema\AddBookResponse.java
com\bookstore\schema\Book.java
com\bookstore\schema\DeleteBookRequest.java
com\bookstore\schema\DeleteBookResponse.java
com\bookstore\schema\GenericResponse.java
com\bookstore\schema\GetBookRequest.java
com\bookstore\schema\GetBookResponse.java
com\bookstore\schema\ObjectFactory.java
com\bookstore\schema\package-info.java

Now build the project using mvn clean package from bookstore-web-service directory  and check that bookstore-web-service.war is created successfully.

11.       Add a provider class BookStoreServiceProvider in a new com.bookstore.core package for servicing add, get and delete book requests. The requests don’t directly come to the provider instead the endpoints forward the request to this provider.
package com.bookstore.core;

import java.util.Hashtable;

import org.springframework.stereotype.Service;

import com.bookstore.schema.Book;

@Service
public class BookStoreServiceProvider {
       Hashtable<String, Object> store = new Hashtable<String, Object>();
      
       public void addBook(Book book) {
              store.put(book.getName(), book);
       }
      
       public void delete(String bookName) {
              store.remove(bookName);
       }
      
       public Book getByName(String name) {
              return (Book) store.get(name);
       }
}


12.       Create endpoints to handle add, get and delete book requests. Here all the endpoints are implemented in a single BookStoreEndpoints class in com.bookstore.endpoints package

package com.bookstore.endpoints;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import com.bookstore.core.BookStoreServiceProvider;
import com.bookstore.schema.AddBookRequest;
import com.bookstore.schema.Book;
import com.bookstore.schema.DeleteBookRequest;
import com.bookstore.schema.GenericResponse;
import com.bookstore.schema.GetBookRequest;
import com.bookstore.schema.GetBookResponse;
import com.bookstore.schema.ObjectFactory;

@Endpoint
public class BookStoreEndpoints {
      
       @Autowired
       BookStoreServiceProvider bookStoreServiceProvider;
      
       ObjectFactory factory = new ObjectFactory();

       @PayloadRoot(localPart="AddBookRequest", namespace="http://www.bookstore.com/schema")
       @ResponsePayload
       public GenericResponse addBook(@RequestPayload AddBookRequest request) {
              Book book = request.getBook();
              bookStoreServiceProvider.addBook(book);
              GenericResponse response = factory.createGenericResponse();
              response.setCode(-1);
              response.setMessage("Book Added Successfully");
              return response;
       }
      
       @PayloadRoot(localPart="DeleteBookRequest", namespace="http://www.bookstore.com/schema")
       @ResponsePayload
       public GenericResponse deleteBook(@RequestPayload DeleteBookRequest request) {
              String bookName = request.getName();
              bookStoreServiceProvider.delete(bookName);
              GenericResponse response = factory.createGenericResponse();
              response.setCode(-1);
              response.setMessage("Book Deleted Successfully");
              return response;
       }

       @PayloadRoot(localPart="GetBookRequest", namespace="http://www.bookstore.com/schema")
       @ResponsePayload
       public GetBookResponse getBook(@RequestPayload GetBookRequest request) {
              String bookName = request.getName();
              Book book = bookStoreServiceProvider.getByName(bookName);
              GetBookResponse response = factory.createGetBookResponse();
              response.setBook(book);
              return response;
       }
}

<--Previous Page                      Next Page-->

No comments :

Post a Comment