JAXWS CXF GlobalWeather + MyEclipse + Maven + Dom4j Byron自學視頻05
使用 Apache CXF 調用 全球氣象 GlobalWeather 提供的 WebService
範例來源: http://www.webservicex.net/globalweather.asmx
Tags: jaxws, cxf, apache, webservice, myeclipse, maven, hello world, java, eclipse, byron, wsdl2java, wsdl first, jetty, tomcat, jaxb, marshal, unmarshal, dom4j, jaxen, global, weather, 氣象
Powered by VideoBam - Free Video Hosting
提及技巧:
* WDSL 內定目錄
* 用 DOM4J 取得温度節點
* 用 Spring 取得 proxy bean
* pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wyd.jaxws</groupId> <artifactId>jaxws-cxf-wsdl</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <cxf.version>2.3.3</cxf.version> </properties> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>${cxf.version}</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <!-- 默認的 產生source檔 存放位置, 故可省略不寫 <sourceRoot>${project.build.directory}/generated-sources/cxf</sourceRoot> --> <!-- 另一種指定 WSDL 檔案的方式 <wsdlOptions> <wsdlOption> <wsdl>${basedir}/src/main/wsdl/ZzhelloWorld.wsdl</wsdl> </wsdlOption> </wsdlOptions> --> <!-- 默認的 WSDL檔 存放位置, 故也可省略不寫 --> <wsdlRoot>${basedir}/src/main/resources/wsdl</wsdlRoot> <includes> <include>*.wsdl</include> </includes> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
* client-beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" 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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="weatherClient" serviceClass="net.webservicex.GlobalWeatherSoap" address="http://www.webservicex.net/globalweather.asmx" /> <!-- 另一種代理工廠方式 <bean id="client" class="net.webservicex.GlobalWeatherSoap" factory-bean="clientFactory" factory-method="create" /> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="net.webservicex.GlobalWeatherSoap" /> <property name="address" value="http://www.webservicex.net/globalweather.asmx" /> </bean> --> </beans>
* WeatherProxyTest.java
package junit.test; import java.util.List; import net.webservicex.GlobalWeatherSoap; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; public class WeatherProxyTest { public static void main(String[] args) { //利用 CXF client端代理工廠 取得 WebService JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); factoryBean.setServiceClass(GlobalWeatherSoap.class); factoryBean.setAddress("http://www.webservicex.net/globalweather.asmx"); GlobalWeatherSoap client = (GlobalWeatherSoap) factoryBean.create(); String citiesXml = client.getCitiesByCountry("taiwan"); String weatherXml = client.getWeather("taipei", "taiwan"); System.out.println(weatherXml); try { //利用 dom4j 將 XML字串 轉為 DOM物件 Document doc = DocumentHelper.parseText(citiesXml); //利用 XPath 定位到 DOM的節點 List<Element> cityList = doc.selectNodes("//Table/City"); //傳回符合的所有節點 for (Element e : cityList) { System.out.println(e.getText()); } Document weatherDoc = DocumentHelper.parseText(weatherXml); Node temperature = weatherDoc.selectSingleNode("//Temperature"); //傳回單一節點 System.out.println("台北温度:" + temperature.getText()); } catch (DocumentException e) { e.printStackTrace(); } } }
* WeatherSpringTest.java
package junit.test; import java.util.List; import net.webservicex.GlobalWeatherSoap; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class WeatherSpringTest { public static void main(String[] args) { //利用 CXF 搭配 Spring 取得 WebService ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml"); GlobalWeatherSoap client = (GlobalWeatherSoap) context.getBean("weatherClient"); String citiesXml = client.getCitiesByCountry("taiwan"); try { //利用 dom4j 將 XML字串 轉為 DOM物件 Document citiesDoc = DocumentHelper.parseText(citiesXml); //利用 XPath 定位到 DOM的節點 List<Element> cityList = citiesDoc.selectNodes("//Table/City"); //傳回符合的所有節點 for (Element city : cityList) { String weatherXml = null; try { weatherXml = client.getWeather(city.getText(), "taiwan"); Document weatherDoc = DocumentHelper.parseText(weatherXml); Node temperature = weatherDoc.selectSingleNode("//Temperature"); //傳回單一節點 System.out.println(city.getText() + ": " + temperature.getText()); } catch (Exception e) { System.out.println("錯了: " + city.getText() + ": " + weatherXml); } } } catch (DocumentException e) { e.printStackTrace(); } } }
* 輸出:
Kangshan Tw-Afb: 73 F (23 C) 錯了: Chinmem / Shatou Air Force Base: Data Not Found Pingtung South Air Force Base: 73 F (23 C) 錯了: Mazu: Data Not Found 錯了: Feng Nin Tw-Afb: Data Not Found 錯了: Chia Tung: Data Not Found 錯了: Taoyuan Ab = 589650: Data Not Found Kaohsiung International Airport: 75 F (24 C) Chiayi Tw-Afb: 68 F (20 C) 錯了: Hengchun: Data Not Found 錯了: Taichung Tw-Afb: Data Not Found 錯了: Dongsha: Data Not Found 錯了: Wuchia Observatory: Data Not Found 錯了: Ilan: Data Not Found 錯了: Tainan Tw-Afb: Data Not Found 錯了: Dongshi: Data Not Found 錯了: Hsinchu Tw-Afb: Data Not Found 錯了: Makung Ab: Data Not Found 錯了: Chihhang Tw-Afb: Data Not Found 錯了: Pingtung North Air Force Base: Data Not Found Sungshan / Taipei: 57 F (14 C) Chiang Kai Shek: 57 F (14 C) 錯了: Pa Kuei / Bakuai: Data Not Found 錯了: Hulien Ab: Data Not Found