5.5. JAX-WS
WebService 支持通过 CXF 组件提供,该组件与也使用 Apache CXF 的 JBoss EAP WebServices 子系统集成。
5.5.1. JAX-WS CXF Producer
以下代码示例使用 CXF 来使用由 WildFly Web 服务子系统部署的 Web 服务。
5.5.1.1. JAX-WS web 服务
以下简单 Web 服务具有一个简单的"greet"方法,它将把两个字符串参数串联在一起并返回它们。
当 JBoss EAP Web 服务子系统检测到包含 JAX-WS 注释的类时,它会引导 CXF 端点。在本例中,服务端点将位于 http://hostname:port/context-root/greeting。
// Service interface
@WebService(name = "greeting")
public interface GreetingService {
@WebMethod(operationName = "greet", action = "urn:greet")
String greet(@WebParam(name = "message") String message, @WebParam(name = "name") String name);
}
// Service implementation
public class GreetingServiceImpl implements GreetingService{
public String greet(String message, String name) {
return message + " " + name ;
}
}5.5.1.2. Camel 路由配置
此 RouteBuilder 配置 CXF producer 端点,该端点将使用上面定义的"greeting"Web 服务。CDI 与 camel-cdi 组件结合使用,用于引导 RouteBuilder 和 CamelContext。
@Startup
@ApplicationScoped
@ContextName("cxf-camel-context")
public class CxfRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:start")
.to("cxf://http://localhost:8080/example-camel-cxf/greeting?serviceClass=" + GreetingService.class.getName());
}
}
greeting web service 'greet' 需要两个参数。它们可以通过 ProducerTemplate 提供给上述路由。Web 服务方法参数值通过构造作为交换正文传递的对象数组来配置。
String message = "Hello"
String name = "Kermit"
ProducerTemplate producer = camelContext.createProducerTemplate();
Object[] serviceParams = new Object[] {message, name};
String result = producer.requestBody("direct:start", serviceParams, String.class);5.5.2. Camel CXF JAX-WS Consumer
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<cxf:cxfEndpoint id="cxfConsumer"
address="http://localhost:8080/webservices/greeting"
serviceClass="org.wildfly.camel.examples.cxf.jaxws.GreetingService" />
<camelContext id="cxfws-camel-context" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="cxf:bean:cxfConsumer" />
<to uri="log:ws" />
</route>
</camelContext>
</beans>5.5.3. 安全性
请参阅 JAX-WS 安全性部分。
5.5.4. EAP 上的 Fuse 中的快速入门示例
在 EAP 安装的 Fuse 中提供了快速入门示例,请访问 quickstarts/camel/camel-cxf-jaxws 目录。