Camel+CXF+Spring转发气象局WebService例子

今天做个Camel转发WebService的例子。实际的WebService提供者选用国家气象局提供的WebService。程序流程为外部系统调用本平台,然后本地平台调用气象局WebService,最后从气象局取得数据后返回给外部系统。本例中因为要调用气象局WebService,所以请先生成气象局WebService所对应的wsdl文件。生成方法网上比较多这里不在赘述。

首先看一下工程的目录结构

cn.com.webxml下一堆文件为根据WeatherWS.wsdl生成文件。

ServiceStart.java  Camel启动程序。

package camel;
import org.apache.camel.spring.Main;

public class ServiceStart {
	public static void main(String[] args) throws Throwable {
		Main main = new Main();
		main.start();
		Thread.sleep(600000);
		main.stop();
	}
}

TestWebService.java 官方自带测试文件(与Camel无关,不需jar就可以测试)

package client;
import java.util.List;
import cn.com.webxml.ArrayOfString;
import cn.com.webxml.WeatherWS;
import cn.com.webxml.WeatherWSSoap;

public class TestWebService {
	public static void main(String[] args) {
		WeatherWS ws = new WeatherWS();
		WeatherWSSoap ww = ws.getWeatherWSSoap();
		ArrayOfString as = ww.getWeather("北京", null);
		List<String> list = as.getString();
		for (String string : list) {
			System.out.println(string);
		}
	}
}

WeatherTest.java 自己写的测试文件,通过转发访问气象局WebService

package client;
import java.util.List;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import cn.com.webxml.ArrayOfString;
import cn.com.webxml.WeatherWSSoap;

public class WeatherTest {
	public static void main(String[] args) {
		WeatherWSSoap client = createCXFClient();
		ArrayOfString aos = //client.getSupportCityString("北京");
//		client.getWeather("792", "");
		client.getRegionCountry();
		List<String> lst = aos.getString();
		for (String str : lst) {
			try{
				String tmp = charSetConvert(str);
				System.out.println(tmp);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	protected static WeatherWSSoap createCXFClient() {
		// we use CXF to create a client for us as its easier than JAXWS and works
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(WeatherWSSoap.class);
		factory.setAddress("http://localhost:9999/test/weatherWebService");
		
		WeatherWSSoap client = (WeatherWSSoap)factory.create();
		
		return client;
	}

	//中文字符处理
	public static String charSetConvert(String xmlRequest){
		String charSet = getEncoding(xmlRequest);
		try {
			byte[] b = xmlRequest.getBytes(charSet);
			xmlRequest = new String(b, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return xmlRequest;
	}
	
	public static String getEncoding(String str) {
		String encode = "GB2312";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) { // 判断是不是GB2312
				String s = encode;
				return s; // 是的话,返回GB2312,以下代码同理
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		encode = "ISO-8859-1";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) { // 判断是不是ISO-8859-1
				String s1 = encode;
				return s1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		encode = "UTF-8";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) { // 判断是不是UTF-8编码
				String s2 = encode;
				return s2;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		encode = "GBK";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) { // 判断是不是GBK
				String s3 = encode;
				return s3;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ""; // 到这一步,你就应该检查是不是其他编码啦
	}
}

spring.xml spring配置文件

<?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:cxf="http://camel.apache.org/schema/cxf"
	xmlns:camel="http://camel.apache.org/schema/spring" xmlns:sec="http://cxf.apache.org/configuration/security"
	xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://camel.apache.org/schema/spring 
		http://camel.apache.org/schema/spring/camel-spring.xsd
		http://camel.apache.org/schema/cxf
		http://camel.apache.org/schema/cxf/camel-cxf.xsd
		http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
		http://cxf.apache.org/configuration/security
		http://cxf.apache.org/schemas/configuration/security.xsd
		http://cxf.apache.org/transports/http/configuration
		http://cxf.apache.org/schemas/configuration/http-conf.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 
		<import resource="classpath:META-INF/cxf/cxf-extension-http-jetty.xml" /> -->

	<!--###### 转发气象局WebService start ######-->
	<cxf:cxfEndpoint id="weatherWebServiceEndpoint"
		address="http://localhost:9999/test/weatherWebService" 
		endpointName="s:WeatherWSSoap"
		serviceName="s:WeatherWS" 
		wsdlURL="etc/WeatherWS.wsdl" 
		xmlns:s="http://WebXml.com.cn/" />
	<!--###### 转发气象局WebService end ######-->

	<camelContext xmlns="http://camel.apache.org/schema/spring">
		<!-- uri为真正的WebService地址 -->
		<endpoint id="callWeatherWebService" uri="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx" />

		<route>
			<from uri="cxf:bean:weatherWebServiceEndpoint?dataFormat=MESSAGE" />
			<removeHeaders pattern="CamelHttp*" />
			<to ref="callWeatherWebService" />
		</route>
	</camelContext>
</beans>

对配置文件说明


pom.xml 需要引入jar

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com</groupId>
	<artifactId>cxfWeather</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>

		<!-- cxf -->
		<!-- used by the real web service -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.1.11</version>
		</dependency>
		<!-- regular http transport -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.1.11</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.1.11</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.19.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-spring</artifactId>
			<version>2.19.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-cxf</artifactId>
			<version>2.19.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-http</artifactId>
			<version>2.19.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.ws.commons.schema</groupId>
			<artifactId>XmlSchema</artifactId>
			<version>1.4.7</version>
		</dependency>

		<dependency>
			<groupId>javax.ws.rs</groupId>
			<artifactId>jsr311-api</artifactId>
			<version>1.1.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.3.8.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-asm</artifactId>
			<version>3.1.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>

	</dependencies>

</project>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值