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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<bean id=“bookingJaxbMarshaller” class=“org.springframework.oxm.jaxb.Jaxb2Marshaller”> <property name=“classesToBeBound”> <list> <value>com.apress.loyalty.ws.BookingRequest</value> <value>com.apress.loyalty.ws.BookingResponse</value> </list> </property> <property name=“marshallerProperties”> <map> <entry key=“jaxb.formatted.output”> <value type=“java.lang.Boolean”>true</value> </entry> </map> </property> </bean> <alias name=“bookingJaxbMarshaller” alias=“bookingJaxbUnmarshaller” /> <bean id=“bookingSoapMessageDispatcher” class=“org.springframework.ws.soap.server.SoapMessageDispatcher”> <property name=“endpointAdapters”> <list> <bean class=“org.springframework.ws.server.endpoint.adapter.MarshallingMethodEndpointAdapter”> <property name=“marshaller” ref=“bookingJaxbMarshaller” /> <property name=“unmarshaller” ref=“bookingJaxbUnmarshaller” /> </bean> </list> </property> </bean> <alias name=“bookingSoapMessageDispatcher” alias=“/loyalty”/> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<bean id=”messageFactory” class=“org.springframework.ws.soap.saaj.SaajSoapMessageFactory” /> <bean id=”webserviceTemplate” class=”org.springframework.ws.client.core.WebserviceTemplate”> <constructor-arg ref=”messageFactory”/> <property name=”defaultUri” value=”http://localhost:8080/loyalty/spring-ws/loyalty/CreateBookingService”/> <property name=”marshaller” value=”bookingJaxbMarshaller”/> <property name=”unmarshaller” value=”bookingJaxbUnmarshaller”/> </bean> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class BookingWebserviceClient{ @Autowired private WebserviceTemplate webserviceTemplate; public void createBookingRequest(){ BookingRequest bookingRequest = …;//Get the object from some factory BookingResponse bookingResponse = (BookingResponse) webserviceTemplate.marshalSendAndReceive(bookingRequest); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<bean id=”validatingInterceptor” class=”org.springframework.ws.client.support.interceptor. PayloadValidatingInterceptor”> <property name=”schema” value=”classpath:/booking.xsd”/> <property name=”validateRequest” value=”true”/> <property name=”validateResponse” value=”true”/> </bean> |
- 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
1 2 3 4 5 |
<bean id=”loggingInterceptor” class=”org.springframework.ws.server.endpoint.interceptor. PayloadLoggingInterceptor”/> |
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<bean id="transformingInterceptor" CodeCxSpMiddle" style="margin-left:64.8pt;mso-add-space:auto"> PayloadTransformingInterceptor"> <property name="requestXslt" value="/WEB-INF/oldRequests.xslt"/> <property name="responseXslt" value="/WEB-INF/oldResponses.xslt"/> </bean> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class MyClientInterceptor implements ClientInterceptor { //…. @Override public boolean handleRequest(MessageContext context) throws WebServiceClientException { //…. return true; } } |
Page Visitors: 10890

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023