5.5. JAX-WS
WebService のサポートは、Apache CXF も使用する JBoss EAP WebServices サブシステムと統合する CXF コンポーネントを介して提供されます。
5.5.1. JAX-WS CXF プロデューサー
以下のコード例では、CXF を使用して WildFly Web サービスサブシステム によってデプロイされた Web サービスを消費します。
5.5.1.1. JAX-WS Web サービス
以下の簡単な Web サービスには、2 つの文字列引数を連結して返す簡単な '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 ; } }
// 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 ;
}
}
Copy to clipboardCopied5.5.1.2. Camel ルートの設定
この RouteBuilder は、上で定義された 'greeting' Web サービスを使用する CXF プロデューサーエンドポイントを設定します。camel-cdi コンポーネントと共に 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()); } }
@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());
}
}
Copy to clipboardCopied
挨拶用 Web サービス 'greet' には 2 つのパラメーターが必要です。これらは、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);
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);
Copy to clipboardCopied5.5.2. Camel CXF JAX-WS コンシューマー
<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>
<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>
Copy to clipboardCopied5.5.3. セキュリティー
JAX-WS security セクションを参照してください。
5.5.4. Fuse on EAP のクイックスタートの例
クイックスタートの例は、quickstarts/camel/camel-cxf-jaxws
ディレクトリーの Fuse on EAP インストールで利用できます。