一:
首先写一个接口,用来发布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); }
在 blueprint.xml 加入如下配置
<cxf:cxfEndpoint id="calcWSEndpoint" serviceClass="com.pp.ws.Calculator" address="http://127.0.0.1:9911/ws/calc" />
三:处理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>
(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个,否则必须加上这个