Spring Book – Chapter 16 – Web Services – Spring WS

XML Marshalling

Using XML marshalling makes it easy to work with object oriented methodologies. spring Ws supports different Object-XML frameworks namely JAXB (1 and 2), Castor, JiBX, XStream and XMLBeans.

In our example we will use XML marshalling using JAXB 2. JAXB classes can be generated from the XSD schema.

Now we will have to configure the XML marshaller and endpoint adapters in the Spring configuration file as shown in Listing 16-7 below.

Listing 16-7. Configuring XML marshaller in Spring configuration file

Implement Client

Following the pattern of Spring in other technologies, it provides WebserviceTemplate similar to JdbcTemplate and JMSTemplate to aid implementing the client for web service consumption and use. Listing 16-8 shows configuring WebserviceTemplate in the Spring configuration file as shown below.

Listing 16-8. Configuring WebserviceTemplate in Spring configuration file

Listing 16-9 below shows the client impementation for accessing the web service we have created.

Listing 16-9. Client code using the WebserviceTemplate class

Detailing WebserviceTemplate in much more detail is out of scope of this book as detailing it would increase this chapter to another 20 pages as Spring WS gives very good way of customizing and integrating with other technologies.

Interceptors

Interceptors are useful for applying specific functionality to required request as required by the application. There are built-in interceptors provided by Spring Framework and if required client interceptors can be written and configured according to application requirement.

I would just like to introduce you to the various interceptors available in the Spring framework which can be used when you have Spring WS in your application. Endpoint interceptors are typically defined by using a <sws:interceptors> element in your Spring application context. In this element, you can simply define endpoint interceptor beans that apply to all endpoints defined in that application context. Alternatively, you can use <sws:payloadRoot> or <sws:soapAction> elements to specify for which payload root name or SOAP action the interceptor should apply.  The various interceptors available are summarized as below:

  • org.springframework.ws.client.support.interceptor.PayloadValidatingInterceptor – validates that request/response XML’s conform to the schema definition. Listing 16-10 shows how to define this interceptor in the Spring configuration file.

Listing 16-10. Defining PayloadValidatingInterceptor in Spring configuration file

  • org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor – logs request/response messages. Listing 16-11 shows declaring this interceptor in Spring configuration file.

Listing 16-11. Defining PayloadLoggingInterceptor in Spring configuration file

  • org.springframework.ws.server.endpoint.interceptor.PayloadTransformingInterceptor – used to transform the payload to another XML format if required by your application. Listing 16-12 shows how the interceptor is defined in the Spring configuration file.

Listing 16-12. Defining PayloadTransformingInterceptor in Spring configuration file

If you would like to define your own interceptors to do your own implementation, you can write your own class by implementing org.springframework.ws.client.support.interceptor.ClientInterceptor as shown in Listing 16-13 below.

Listing 16-13. Custom interceptor class implementing ClientInterceptor

Page Visitors: 11141

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 *