Java Reference
In-Depth Information
Invoking Web Services with XML Marshalling
A Spring-WS client can also marshal/unmarshal the request and response objects to/from XML
messages. As an example, I will create a client using Castor as the marshaller so that you can reuse the
object models GetTemperaturesRequest , GetTemperaturesResponse , and TemperatureInfo , and also
the mapping configuration file, mapping.xml , from the service endpoint.
Let's implement the service proxy with XML marshalling. WebServiceTemplate provides a
marshalSendAndReceive() method that accepts a request object as the method argument, which
will be marshalled to the request message. This method has to return a response object that will be
unmarshalled from the response message.
Note To create a service client using Castor, you have to include castor-1.2.jar (located in the lib/castor
directory of the Spring-WS installation) and xercesImpl-2.8.1.jar (located in lib/xerces ) in your classpath.
package com.apress.springenterpriserecipes.weather;
...
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
public class WeatherServiceProxy extends WebServiceGatewaySupport
implements WeatherService {
public List<TemperatureInfo> getTemperatures(String city, List<Date> dates) {
GetTemperaturesRequest request = new GetTemperaturesRequest(city, dates);
GetTemperaturesResponse response = (GetTemperaturesResponse)
getWebServiceTemplate().marshalSendAndReceive(request);
return response.getTemperatures();
}
}
When using XML marshalling, WebServiceTemplate requires both the marshaller and unmarshaller
properties to be set. You can also set them to WebServiceGatewaySupport if you extend this class to have
WebServiceTemplate auto-created. Usually, you can specify a single marshaller for both properties. For
Castor, you declare a CastorMarshaller bean as the marshaller.
<beans ...>
<bean id="client"
class="com.apress.springenterpriserecipes.weather.WeatherServiceClient">
<property name="weatherService" ref="weatherServiceProxy" />
</bean>
<bean id="weatherServiceProxy"
class="com.apress.springenterpriserecipes.weather.WeatherServiceProxy">
<property name="defaultUri"
value=" http://localhost:8080/weather/services" />
<property name="marshaller" ref="marshaller" />
<property name="unmarshaller" ref="marshaller" />
</bean>
Search WWH ::




Custom Search