Spring Book – Chapter 16 – Web Services – Spring WS

Spring WS and Spring Framework

Before getting onto Spring WS in detail, it’s wise to know how Spring WS relate to other Spring Framework’s portfolio mainly Spring Core and Spring MVC.

The programming model of Spring WS is very much similar to Spring MVC. You have the role of a Controller in Spring MVC, which takes input parameters from a web request and invokes some kind of business service with those parameters to render a response. Similarly, in Spring WS You define an end-point that looks at the incoming XML message using whatever API you want, it invokes a business class or POJO (Plain old Java object), and given the return value from that POJO, it generates a response. The use of technology to do can be according to the requirement that you have in your application.

In addition to the above, there is also tight integration with Spring-Security, which is the official the enterprise security framework. Using plain Spring Security, you can authenticate providing user with a normal login dialog and with Spring WS you can authenticate using ws-security, after which you can put the credentials of the user inside the SOAP header for propagation. All the single-sign-on capabilities which Spring Security supports can be used as authentication mechanism by Spring WS. Using this capability Spring WS can authenticate user against any LDAP directory or Windows Active Directory and any kind of single sign-on solution with ease.

Spring WS modules

Figure 16-7 shows the dependency and interaction model between the various modules in Spring WS.

 Figure 16-7. Spring WS modules

Figure 16-7. Spring WS modules

The various modules of Spring WS can be summarized as below:

  • Spring XML – contains various XML support classes for Spring Web Services. This is a module intended to be used by the Spring WS framework and not for the web service developers. Packaged as spring-xml.jar.
  • Spring OXM – Spring WS uses this module contained in Spring Framework 3.x for achieving various object-XML mapping capabilities.
  • Spring WS Core – central to Spring WS’s functionality. Contains central interfaces like WebServiceMessage and SoapMessage, various support classes for implementing web service endpoints and WebServiceTemplate class. Packaged as spring-ws-core.jar.
  • Spring WS Support – support module which contains additional transport support like JMS, Email etc. Packaged as spring-ws-support.jar.
  • Spring WS Security – provides web service security implementation classes. Packaged as spring-ws-security.jar.

Spring WS Architecture

The overall architecture of Spring WS is shown in Figure 16-8.

 Figure 16-8. Spring WS architecture

Figure 16-8. Spring WS architecture

Spring Web Service Example

To understand Spring WS, let’s take an example. We would like to expose a service which allows booking of cargo which will be flown by air from a particular origin to a particular destination. We will now do an end-to-end implementation of this service and here are the various steps that we will do in the due course of implementing this service:

  1. Design the Service
  2. Design XSD
  3. Design WSDL
  4. Implement the actual service
  5. Configure Spring WS
  6. Configure MessageDispatcherServlet in web.xml
  7. In Spring configuration file, do the following:
  • Specify the schema
  • Publish the WSDL
  • Configure Endpoints
  1. Handle Messages
  2. XML Marshalling
  3. Implement Client using Spring WS

Design the Service

First of all we will design the required XSD for the web service as shown in Listing 16-1 below. All the types used in the service should be defined in XSD schema.

Listing 16-1. Cargo booking service XSD schema – booking.xsd

Now that XSD is designed, we will design the WSDL. In WSDL, define the services based on the messages defined in the XSD schema. Everything is derived from the XSD. There is a provision of generating WSDL using Spring WS. Listing 16-2 shows manual WSDL creation using XSD schema created previously.

Listing 16-2. Desigining WSDL manually

Page Visitors: 9390

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

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