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(
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=""
xsi:schemaLocation=" ">
<bean id="client"
<property name="weatherService" ref="weatherServiceProxy" />
<bean id="weatherServiceProxy"
<property name="webServiceTemplate" ref="webServiceTemplate" />
<bean id="webServiceTemplate"
<property name="defaultUri"
value=" http://localhost:8080/weather/services" />
Search WWH ::

Custom Search