5.4. JAX-RS
JAX-RS 支持由 Camel CXF-RS 提供。
5.4.1. CXF-RS Producer 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
<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 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: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 Consumer 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
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 canmarshal 和 unmarshal JSON 数据可以通过指定 'produces()' 或 'type()' 配置步骤。
注意
-
REST DSL 配置从
restConfiguration().component("undertow")
开始。 - EAP 子系统上的 Camel 仅支持 camel-servlet 和 camel-undertow 组件,以用于 REST DSL。但是,如果您配置其他组件,它将无法正常工作。
5.4.4. 安全性 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
请参阅 JAX-RS 安全性部分。
5.4.5. EAP 上的 Fuse 中的 Quickstart 示例 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
在快速入门 /camel/camel-cxf-jaxrs 目录的 Fuse 中提供了快速入门
示例。