5.4. JAX-RS
JAX-RS のサポートは Camel CXF-RS によって提供されます。
5.4.1. CXF-RS プロデューサー
<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:rsClient id="cxfProducer" address="http://localhost:8080/rest" serviceClass="org.wildfly.camel.examples.cxf.jaxrs.GreetingService" /> <camelContext id="cxfrs-camel-context" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start" /> <setHeader headerName="operationName"> <simple>greet</simple> </setHeader> <setHeader headerName="CamelCxfRsUsingHttpAPI"> <constant>false</constant> </setHeader> <to uri="cxfrs:bean:cxfProducer" /> </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:rsClient id="cxfProducer"
address="http://localhost:8080/rest"
serviceClass="org.wildfly.camel.examples.cxf.jaxrs.GreetingService" />
<camelContext id="cxfrs-camel-context" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:start" />
<setHeader headerName="operationName">
<simple>greet</simple>
</setHeader>
<setHeader headerName="CamelCxfRsUsingHttpAPI">
<constant>false</constant>
</setHeader>
<to uri="cxfrs:bean:cxfProducer" />
</route>
</camelContext>
</beans>
5.4.2. CXF-RS コンシューマー
<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:rsServer id="cxfConsumer" address="http://localhost:8080/rest" serviceClass="org.wildfly.camel.examples.cxf.jaxrs.GreetingService" /> <camelContext id="cxfrs-camel-context" xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="cxfrs:bean:cxfConsumer" /> <setBody> <constant>Hello world</constant> </setBody> </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:rsServer id="cxfConsumer"
address="http://localhost:8080/rest"
serviceClass="org.wildfly.camel.examples.cxf.jaxrs.GreetingService" />
<camelContext id="cxfrs-camel-context" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="cxfrs:bean:cxfConsumer" />
<setBody>
<constant>Hello world</constant>
</setBody>
</route>
</camelContext>
</beans>
5.4.3. Camel REST DSL による JAX-RS コンシューマー
Camel REST DSL により、JAX-RS コンシューマーとして動作する Camel ルートを書き込むことができます。以下の RouteBuilder クラスがこれを示しています。
@Startup @ApplicationScoped @ContextName("rest-camel-context") public class RestConsumerRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { // Use the camel-undertow component to provide REST integration restConfiguration().component("undertow") .contextPath("/rest").port(8080).bindingMode(RestBindingMode.json); rest("/customer") // GET /rest/customer .get() .produces(MediaType.APPLICATION_JSON) .to("direct:getCustomers") // GET /rest/customer/1 .get("/{id}") .produces(MediaType.APPLICATION_JSON) .to("direct:getCustomer") // POST /rest/customer .post() .type(Customer.class) .to("direct:createCustomer"); // PUT /rest/customer .put() .type(Customer.class) .to("direct:updateCustomer"); // DELETE /rest/customer/1 .delete("/{id}") .to("direct:deleteCustomer"); } }
@Startup
@ApplicationScoped
@ContextName("rest-camel-context")
public class RestConsumerRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
// Use the camel-undertow component to provide REST integration
restConfiguration().component("undertow")
.contextPath("/rest").port(8080).bindingMode(RestBindingMode.json);
rest("/customer")
// GET /rest/customer
.get()
.produces(MediaType.APPLICATION_JSON)
.to("direct:getCustomers")
// GET /rest/customer/1
.get("/{id}")
.produces(MediaType.APPLICATION_JSON)
.to("direct:getCustomer")
// POST /rest/customer
.post()
.type(Customer.class)
.to("direct:createCustomer");
// PUT /rest/customer
.put()
.type(Customer.class)
.to("direct:updateCustomer");
// DELETE /rest/customer/1
.delete("/{id}")
.to("direct:deleteCustomer");
}
}
バインディングモードを設定することで、Camel は 'produces()' または 'type()' 設定ステップのいずれかを指定することにより、JSON データをマーシャリングおよびアンマーシャリングできます。
注記
-
REST DSL 設定は、
restConfiguration().component("undertow")
で始まります。 - Camel on EAP サブシステムは、REST DSL で使用する camel-servlet および camel-undertow コンポーネントのみをサポートします。他のコンポーネントを設定した場合は機能しません。
5.4.4. セキュリティー
JAX-RS のセキュリティー セクションを参照してください。
5.4.5. Fuse on EAP のクイックスタートの例
クイックスタートの例は、quickstarts/camel/camel-cxf-jaxrs
ディレクトリーの Fuse on EAP インストールで利用できます。