Java Reference
In-Depth Information
Note To invoke a web service using Spring-WS, you have to include spring-ws-1.5.8.jar (located
in the dist directory of the Spring-WS installation), stax-api-1.0.2.jar (located in lib/stax ),
activation-1.1.1.jar and saaj-api-1.3.jar (located in lib/java-ee ), and saaj-impl-1.3.jar (located in
lib/saaj ) in your classpath. To use dom4j, you also have to include dom4j-1.6.1.jar (located in lib/dom4j ).
package com.apress.springenterpriserecipes.weather;
...
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
import org.springframework.ws.client.core.WebServiceTemplate;
public class WeatherServiceProxy implements WeatherService {
private static final String namespaceUri =
" http://springenterpriserecipes.apress.com/weather/schemas" ;
private DateFormat dateFormat;
private WebServiceTemplate webServiceTemplate;
public WeatherServiceProxy() throws Exception {
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
}
public void setWebServiceTemplate(WebServiceTemplate webServiceTemplate) {
this.webServiceTemplate = webServiceTemplate;
}
public List<TemperatureInfo> getTemperatures(String city, List<Date> dates) {
// Build the request document from the method arguments
Document requestDocument = DocumentHelper.createDocument();
Element requestElement = requestDocument.addElement(
"GetTemperaturesRequest", namespaceUri);
requestElement.addElement("city").setText(city);
for (Date date : dates) {
requestElement.addElement("date").setText(dateFormat.format(date));
}
// Invoke the remote web service
DocumentSource source = new DocumentSource(requestDocument);
DocumentResult result = new DocumentResult();
webServiceTemplate.sendSourceAndReceiveToResult(source, result);
Search WWH ::




Custom Search