使用Apache Camel处理web service请求的三种方法
2015-09-30 09:53
561 查看
一:
首先写一个接口,用来发布web service
二:发布web service
在 blueprint.xml 加入如下配置
然后,访问http://127.0.0.1:9911/ws/calc?wsdl 验证是否发布成功
三:处理web service请求,有以下三种方法
(1)
写一个processor 实现org.apache.camel.Processor 接口去处理ws请求
路由配置如下:
(2)
写一个类,有N(N为接口的方法个数)个方法,每个方法的参数均为 org.apache.camel.Exchange
路由配置如下:
这里主要使用的是CamelBeanMethodName来动态区分需要执行的方法
(3)
写一个上面接口的实现类,去处理ws的请求
路由配置如下:
这里也是使用的是CamelBeanMethodName来动态区分需要执行的方法
注意上面的CamelBeanMultiParameterArray,除非接口的所有方法的参数个数都是1个,否则必须加上这个
首先写一个接口,用来发布web service
package com.pp.ws; import javax.jws.WebMethod; import javax.jws.WebService; /** * 计算器接口 */ @WebService public interface Calculator { @WebMethod public String getVersion(String request); @WebMethod public Long add(Integer a, Integer b); @WebMethod public Long subtract(Integer a, Integer b); @WebMethod public Long multiply(Integer a, Integer b); }
二:发布web service
在 blueprint.xml 加入如下配置
<cxf:cxfEndpoint id="calcWSEndpoint" serviceClass="com.pp.ws.Calculator" address="http://127.0.0.1:9911/ws/calc" />
然后,访问http://127.0.0.1:9911/ws/calc?wsdl 验证是否发布成功
三:处理web service请求,有以下三种方法
(1)
写一个processor 实现org.apache.camel.Processor 接口去处理ws请求
package com.pp.ws; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.cxf.message.MessageContentsList; public class CalculatorProcessor implements Processor { /** * 根据operatioNname 去执行相应的逻辑,然后设置相应的返回值(一定要和接口的返回值一致) */ public void process(Exchange exchange) throws Exception { MessageContentsList args = exchange.getIn().getBody(MessageContentsList.class); String method = exchange.getIn().getHeader("operatioNname", String.class); if("getVersion".equals(method)) { exchange.getOut().setBody("2.14.1"); return; } Object arg1 = args.get(0); Object arg2 = args.get(1); Integer a = Integer.valueOf(arg1.toString()); Integer b = Integer.valueOf(arg2.toString()); if("add".equals(method)) { exchange.getOut().setBody(Long.valueOf(a + b)); } else if("subtract".equals(method)) { exchange.getOut().setBody(Long.valueOf(a - b)); } else if("multiply".equals(method)) { exchange.getOut().setBody(Long.valueOf(a * b)); } } }
路由配置如下:
<bean id="calcProcessor" class="com.pp.ws.CalculatorProcessor" /> <route> <from uri="calcWSEndpoint" /> <to uri="bean:calcProcessor" /> </route>
(2)
写一个类,有N(N为接口的方法个数)个方法,每个方法的参数均为 org.apache.camel.Exchange
package com.pp.ws; import org.apache.camel.Exchange; import org.apache.cxf.message.MessageContentsList; public class CalculatorMutProcessor { /** * 使用Exchange作为参数的时候,方法不能有返回值,下面亦同 * 注意:exchange.getOut().setBody()的值类型必须和接口方法返回的类型保持一致 */ public void add(Exchange exchange) { MessageContentsList args = exchange.getIn().getBody(MessageContentsList.class); Integer a = Integer.valueOf(args.get(0).toString()); Integer b = Integer.valueOf(args.get(1).toString()); exchange.getOut().setBody(Long.valueOf(a + b)); } public void subtract(Exchange exchange) { MessageContentsList args = exchange.getIn().getBody(MessageContentsList.class); Integer a = Integer.valueOf(args.get(0).toString()); Integer b = Integer.valueOf(args.get(1).toString()); exchange.getOut().setBody(Long.valueOf(a - b)); } public void multiply(Exchange exchange) { MessageContentsList args = exchange.getIn().getBody(MessageContentsList.class); Integer a = Integer.valueOf(args.get(0).toString()); Integer b = Integer.valueOf(args.get(1).toString()); exchange.getOut().setBody(Long.valueOf(a * b)); } public void getVersion(Exchange exchange) { exchange.getOut().setBody("2.14.1"); } }
路由配置如下:
<bean id="calculatorMutProcessor" class="com.pp.ws.CalculatorMutProcessor" /> <route> <from uri="calcWSEndpoint" /> <setHeader headerName="CamelBeanMethodName"> <simple>${in.header.operatioNname}</simple> </setHeader> <to uri="bean:calculatorMutProcessor" /> </route>
这里主要使用的是CamelBeanMethodName来动态区分需要执行的方法
(3)
写一个上面接口的实现类,去处理ws的请求
package com.pp.ws; public class CalculatorImpl implements Calculator { public String getVersion(String request) { System.out.println(request); return "2.14.1"; } public Long add(Integer a, Integer b) { System.out.println(a + ", " + b); return Long.valueOf(a + b); } public Long subtract(Integer a, Integer b) { System.out.println(a + ", " + b); return Long.valueOf(a - b); } public Long multiply(Integer a, Integer b) { System.out.println(a + ", " + b); return Long.valueOf(a * b); } }
路由配置如下:
<bean id="calculatorImpl" class="com.pp.ws.CalculatorImpl" /> <route> <from uri="calcWSEndpoint" /> <setHeader headerName="CamelBeanMethodName"> <simple>${in.header.operationName}</simple> </setHeader> <setHeader headerName="CamelBeanMultiParameterArray"> <constant>true</constant> </setHeader> <to uri="bean:calculatorImpl" /> </route>
这里也是使用的是CamelBeanMethodName来动态区分需要执行的方法
注意上面的CamelBeanMultiParameterArray,除非接口的所有方法的参数个数都是1个,否则必须加上这个
相关文章推荐
- Spark java.lang.UnsatisfiedLinkError: org.apache.hadoop.util.NativeCrc32
- apache vhost配置
- BUILD问题“Class not found : Lorg/apache/commons/logging/Log”
- 写得很详细的apache的入门文档
- 安装潮流组合--LAMP+NFS(编译php-fpm模块与apache2.4结合)
- ubuntu14.0(linux) 安装 apache2.4+ 报 apr not found
- 五种开源协议(GPL,LGPL,BSD,MIT,Apache)
- Apache Spark 1.5发布,新特性一览
- Web性能压力测试工具之ApacheBench(ab)详解
- 浏览器、apache的连接超时详解
- Apache性能优化、超时设置,linux 重启apache
- 让时间处理简单化 【第三方扩展类库org.apache.commons.lang.time】
- Apache Maven 入门篇(下)
- Apache Ignite——新一代数据库缓存系统
- Apache Ignite——新一代数据库缓存系统
- Apache Maven入门篇(上)
- LAMP编译安装之PHP以fpm的方式结合apache工作
- Apache+PHP+VC14环境搭建及测试
- apache 虚拟主机
- apache搭建完成测试PHP页面