Added weather forecast sample
This commit is contained in:
@@ -16,6 +16,7 @@ additional instructions.
|
||||
- [mtom](./mtom) - shows how to use MTOM and JAXB2 marshalling
|
||||
- [stockquote](./stockquote) - shows how to use WS-Addressing and the Java 6 HTTP Server
|
||||
- [tutorial](./tutorial) - contains the code from the Spring-WS tutorial
|
||||
- [weather](./weather) - shows how to connect to a public SOAP service
|
||||
|
||||
## Running the Server
|
||||
|
||||
|
||||
19
weather/README.md
Normal file
19
weather/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Weather Forecast Sample
|
||||
|
||||
This sample shows how to use Spring Web Services to connect to the [CDYNE Weather Service].
|
||||
It uses JAXB to generate classes from the service's WSDL, and the `WebServiceTemplate` to
|
||||
send the request to and receive the response from the service.
|
||||
|
||||
You can run the client by using the following command from within this directory:
|
||||
|
||||
```sh
|
||||
$ gradle runClient
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
[Spring Web Services] is released under version 2.0 of the [Apache License].
|
||||
|
||||
[Spring Web Services]: http://projects.spring.io/spring-ws
|
||||
[Apache License]: http://www.apache.org/licenses/LICENSE-2.0
|
||||
[CDYNE Weather Service]: http://wiki.cdyne.com/index.php/CDYNE_Weather
|
||||
65
weather/build.gradle
Normal file
65
weather/build.gradle
Normal file
@@ -0,0 +1,65 @@
|
||||
configurations {
|
||||
jaxb
|
||||
}
|
||||
|
||||
ext.springVersion = '3.2.4.RELEASE'
|
||||
ext.springWsVersion = '2.1.4.RELEASE'
|
||||
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'eclipse'
|
||||
apply plugin: 'idea'
|
||||
|
||||
repositories {
|
||||
maven { url 'http://repo.spring.io/libs-release' }
|
||||
}
|
||||
|
||||
task genJaxb {
|
||||
ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
|
||||
ext.classesDir = "${buildDir}/classes/jaxb"
|
||||
ext.schema = "http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl"
|
||||
|
||||
outputs.dir classesDir
|
||||
|
||||
doLast() {
|
||||
project.ant {
|
||||
taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
|
||||
classpath: configurations.jaxb.asPath
|
||||
mkdir(dir: sourcesDir)
|
||||
mkdir(dir: classesDir)
|
||||
|
||||
xjc(destdir: sourcesDir, schema: schema,
|
||||
package: "org.springframework.ws.samples.weather") {
|
||||
arg(value: "-wsdl")
|
||||
produces(dir: sourcesDir, includes: "**/*.java")
|
||||
}
|
||||
|
||||
javac(destdir: classesDir, source: 1.6, target: 1.6, debug: true,
|
||||
debugLevel: "lines,vars,source",
|
||||
classpath: configurations.jaxb.asPath) {
|
||||
src(path: sourcesDir)
|
||||
include(name: "**/*.java")
|
||||
include(name: "*.java")
|
||||
}
|
||||
|
||||
copy(todir: classesDir) {
|
||||
fileset(dir: sourcesDir, erroronmissingdir: false) {
|
||||
exclude(name: "**/*.java")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile("org.springframework.ws:spring-ws-core:$springWsVersion")
|
||||
compile(files(genJaxb.classesDir).builtBy(genJaxb))
|
||||
|
||||
runtime("log4j:log4j:1.2.16")
|
||||
|
||||
jaxb "com.sun.xml.bind:jaxb-xjc:2.1.7"
|
||||
}
|
||||
|
||||
task runClient(dependsOn: 'classes', type:JavaExec) {
|
||||
main = "org.springframework.ws.samples.weather.WeatherClient"
|
||||
classpath = sourceSets.main.runtimeClasspath
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.samples.weather;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
|
||||
import org.springframework.ws.soap.client.core.SoapActionCallback;
|
||||
|
||||
public class WeatherClient extends WebServiceGatewaySupport {
|
||||
|
||||
public GetCityForecastByZIPResponse getCityForecastByZip(String zipCode) {
|
||||
GetCityForecastByZIP request = new GetCityForecastByZIP();
|
||||
request.setZIP(zipCode);
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Requesting forecast for " + zipCode);
|
||||
|
||||
GetCityForecastByZIPResponse response =
|
||||
(GetCityForecastByZIPResponse) getWebServiceTemplate()
|
||||
.marshalSendAndReceive(request, new SoapActionCallback(
|
||||
"http://ws.cdyne.com/WeatherWS/GetCityForecastByZIP"));
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public void printResponse(GetCityForecastByZIPResponse response) {
|
||||
ForecastReturn forecastReturn = response.getGetCityForecastByZIPResult();
|
||||
|
||||
|
||||
if (forecastReturn.isSuccess()) {
|
||||
System.out.println();
|
||||
System.out.println("Forecast for " + forecastReturn.getCity() + ", " +
|
||||
forecastReturn.getState());
|
||||
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
for (Forecast forecast : forecastReturn.getForecastResult().getForecast()) {
|
||||
System.out.print(format
|
||||
.format(forecast.getDate().toGregorianCalendar().getTime()));
|
||||
System.out.print(" ");
|
||||
System.out.print(forecast.getDesciption());
|
||||
System.out.print(" ");
|
||||
Temp temperature = forecast.getTemperatures();
|
||||
System.out.print(temperature.getMorningLow() + "\u00b0-" +
|
||||
temperature.getDaytimeHigh() + "\u00b0 ");
|
||||
System.out.println();
|
||||
}
|
||||
} else {
|
||||
System.out.println("No forecast received");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("applicationContext.xml",
|
||||
WeatherClient.class);
|
||||
WeatherClient client = context.getBean("weatherClient", WeatherClient.class);
|
||||
|
||||
String zipCode = "94304";
|
||||
if (args.length > 0) {
|
||||
zipCode = args[0];
|
||||
}
|
||||
|
||||
GetCityForecastByZIPResponse response = client.getCityForecastByZip(zipCode);
|
||||
client.printResponse(response);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
7
weather/src/main/resources/log4j.properties
Normal file
7
weather/src/main/resources/log4j.properties
Normal file
@@ -0,0 +1,7 @@
|
||||
log4j.rootLogger=WARN, stdout
|
||||
log4j.logger.org.springframework.ws=DEBUG
|
||||
log4j.logger.org.springframework.xml=DEBUG
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:oxm="http://www.springframework.org/schema/oxm"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd">
|
||||
|
||||
<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.weather"/>
|
||||
|
||||
<bean id="weatherClient" class="org.springframework.ws.samples.weather.WeatherClient">
|
||||
<property name="defaultUri" value="http://wsf.cdyne.com/WeatherWS/Weather.asmx"/>
|
||||
<property name="marshaller" ref="marshaller"/>
|
||||
<property name="unmarshaller" ref="marshaller"/>
|
||||
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user