Book Image

Spring Web Services 2 Cookbook

By : Hamidreza Sattari, Shameer Kunjumohamed
Book Image

Spring Web Services 2 Cookbook

By: Hamidreza Sattari, Shameer Kunjumohamed

Overview of this book

<p>Spring Web Services is a product of the Spring community focused on creating document-driven Web services.<br /><br />Spring Web Services aims to facilitate contract-first SOAP service development, allowing for the creation of flexible web services using one of the many ways to manipulate XML payloads.<br /><br />This comprehensive guide -- which provides professional expertise on a variety of technical topics right from setting-up a contract-first Web-Service, creating client of a Web-Service to serialization, monitoring, testing and security using Spring-WS -- helps you enhance your skills in Spring Web Services.<br /><br />Spring Web Services 2 Cookbook includes a wide variety of recipes that covers most important topics used in real-world applications. It is a well-rounded guide covering a lot of ground in the Spring Web Services domain using systematic arranged chapters and focused recipes.<br /><br />The book begins with setting up a contract first Web Service over various protocols such as JMS, XMPP, and Email. The next chapter targets creating clients for SOAP Web Services. We then learn how to test and monitor the Web Service using tools like soapUI and TCPMon. Building on, logging, tracing and exception handling are detailed in the subsequent chapter. The book then covers marshalling and unmarshalling using different technologies like JAXB2, XMLBeans, JibX, XStream, MooseXML etc. Securing WebServices through authentication, authorization, encryption and decryption and digital signature using Spring-WS features based on XWSS and WSS4J Libraries is outlined in the next chapter two chapters. The book then tackles development of RESTful Web Services. Finally, Setting up Web Services using Spring Remoting based on various technologies like HTTP , RMI, JMS, JAXWS and a Web Service using Apache CXF on JAX-WS front-end are explained.<br /><br />This book will help relatively new developers in accelerating their learning process and experienced developers in expanding their skills sets of Spring Web Services.</p>
Table of Contents (17 chapters)
Spring Web Services 2 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up a simple endpoint mapping for the Web-Service


This recipe demonstrates a very simple endpoint mapping that maps a Web-Service request to a Java class method.

Getting ready

In this recipe, the project's name is LiveRestaurant_R-1.9, with the following Maven dependencies:

  • spring-ws-core-2.0.1.RELEASE.jar

  • log4j-12.9.jar

How to do it...

The steps of this recipe are the same as that of the previous recipe, Setting up a contract-first Web-Service, except that the registration of the endpoint, that is, method endpoint mapping and is configured in spring-ws-servlet.xml.

  1. Define an endpoint (OrderSeviceMethodEndpoint) based on the method mapping standard (SimpleMethodEndpointMapping).

  2. Configure the method endpoint mapping in spring-ws-servlet.xml.

  3. Run the mvn clean package tomcat:run command and browse to see the WSDL:

    http://localhost:8080/LiveRestaurant/OrderService.wsdl
  4. To test, open a new command window, go to Liverestaurant_R-1.9-Client, and run the following command:

    mvn clean package exec:java
    

    Here is the server-side output:

    Sent response ..
    <tns:placeOrderResponse xmlns:tns="..."><tns:refNumber>order-John_Smith_1234</tns:refNumber>
    </tns:placeOrderResponse>...
     for request ...
    <tns:placeOrderRequest xmlns:tns="...">
      <tns:order>
        <tns:refNumber>order-9999</tns:refNumber>
        <tns:customer>
         ........
        </tns:customer>
        <tns:dateSubmitted>2008-09-29T05:49:45</tns:dateSubmitted>
        <tns:orderDate>2014-09-19T03:18:33</tns:orderDate>
        <!--1 or more repetitions:-->
        <tns:items>
          <tns:type>Snacks</tns:type>
          <tns:name>Pitza</tns:name>
          <tns:quantity>2</tns:quantity>
        </tns:items>
      </tns:order>
    </tns:placeOrderRequest>
    

How it works...

SimpleMethodEndpointMapping maps from the local name of the request payload (placeOrderRequest) to the methods of the POJO classes. Here is a sample of the request payload (note the local name of the request payload):

</tns:placeOrderRequest>
  <tns:order>
......
  </tns:order>
</tns:placeOrderRequest>

The endpoint bean is registered using the endpoints property. This property tells you that there should be a method in the endpoint class (OrderServiceEndpoint) with a name that starts with methodPrefix(handle) and ends with the request payload local name (placeOrderRequest). This increases the flexibility of the endpoint naming by using the configuration in spring-ws-servlet.xml:

  <bean class="org.springframework.ws.server.endpoint.mapping.SimpleMethodEndpointMapping">
    <property name="endpoints">
      <ref bean="OrderServiceEndpoint"/>
    </property>
    <property name="methodPrefix" value="handle"></property>
    <property name="interceptors">
      <list>
        <bean
          class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor">
          <property name="logRequest" value="true" />
          <property name="logResponse" value="true" />
        </bean>
      </list>
    </property>
  </bean>
  <bean id="OrderServiceEndpoint" class="com.packtpub.liverestaurant.service.endpoint.OrderSeviceMethodEndpoint">
  </bean>

The endpoint method name should match the handle+request message root name (handleplaceOrderRequest). In the body of the method, we should process the request and finally return the response in the form of javax.xml.transform.Source:

public class OrderSeviceMethodEndpoint {
  private OrderService orderService;
  @Autowired
    public void setOrderService(OrderService orderService) {
    this.orderService = orderService;
  }    
  public @ResponsePayload Source handleplaceOrderRequest(@RequestPayload Source source) throws Exception {
    //extract data from input parameter
    String fName="John";
    String lName="Smith";
    String refNumber="1234";    
      return new StringSource(
    "<tns:placeOrderResponse xmlns:tns=\"http://www.packtpub.com/liverestaurant/OrderService/schema\"><tns:refNumber>"+orderService.placeOrder(fName, lName, refNumber)+"</tns:refNumber></tns:placeOrderResponse>");
  }
}

See also

The recipes Setting up a transport-neutral WS-Addressing endpoint and Setting up an endpoint by annotating the payload-root, discussed in this chapter.