Java Reference
In-Depth Information
// Extract the result from the response document
Document responsetDocument = result.getDocument();
Element responseElement = responsetDocument.getRootElement();
List<TemperatureInfo> temperatures = new ArrayList<TemperatureInfo>();
for (Object node : responseElement.elements("TemperatureInfo")) {
Element element = (Element) node;
try {
Date date = dateFormat.parse(element.attributeValue("date"));
double min = Double.parseDouble(element.elementText("min"));
double max = Double.parseDouble(element.elementText("max"));
double average = Double.parseDouble(
element.elementText("average"));
temperatures.add(
new TemperatureInfo(city, date, min, max, average));
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
return temperatures;
}
}
In the getTemperatures() method, you first build the request message using the dom4j
API. WebServiceTemplate provides a sendSourceAndReceiveToResult() method that accepts a
java.xml.transform.Source and a java.xml.transform.Result object as arguments. You have to build a
dom4j DocumentSource object to wrap your request document, and create a new dom4j DocumentResult
object for the method to write the response document to it. Finally, you get the response message and
extract the results from it.
With the service proxy written, you can declare it in a client bean configuration file such as
client.xml . Because this proxy requires an instance of WebServiceTemplate for sending and receiving the
messages, you have to instantiate it and inject this instance into the proxy. Also, you specify the default
service URI for the template so that all the requests will be sent to this URI by default.
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
<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="webServiceTemplate" ref="webServiceTemplate" />
</bean>
<bean id="webServiceTemplate"
class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="defaultUri"
value=" http://localhost:8080/weather/services" />
</bean>
</beans>
Search WWH ::




Custom Search