79.7. 消耗 REST 请求 - 默认绑定 Style
CXF JAXRS 前端 实施 JAX-RS (rust-311) API,因此我们可以将资源类导出为 REST 服务。我们利用 CXF Invoker API 将 REST 请求转换为普通的 Java 对象方法调用。与 Camel Restlet 组件不同,您不需要在端点中指定 URI 模板,CXF 根据 JSR-311 规范,处理 REST 请求 URI 到资源类方法映射。您需要在 Camel 中完成的所有操作,将此方法请求委托给正确的处理器或端点。
以下是 CXFRS route… 的示例
private static final String CXF_RS_ENDPOINT_URI =
"cxfrs://http://localhost:" + CXT + "/rest?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerServiceResource";
private static final String CXF_RS_ENDPOINT_URI2 =
"cxfrs://http://localhost:" + CXT + "/rest2?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerService";
private static final String CXF_RS_ENDPOINT_URI3 =
"cxfrs://http://localhost:" + CXT + "/rest3?"
+ "resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerServiceNoAnnotations&"
+ "modelRef=classpath:/org/apache/camel/component/cxf/jaxrs/CustomerServiceModel.xml";
private static final String CXF_RS_ENDPOINT_URI4 =
"cxfrs://http://localhost:" + CXT + "/rest4?"
+ "modelRef=classpath:/org/apache/camel/component/cxf/jaxrs/CustomerServiceDefaultHandlerModel.xml";
private static final String CXF_RS_ENDPOINT_URI5 =
"cxfrs://http://localhost:" + CXT + "/rest5?"
+ "propagateContexts=true&"
+ "modelRef=classpath:/org/apache/camel/component/cxf/jaxrs/CustomerServiceDefaultHandlerModel.xml";
protected RouteBuilder createRouteBuilder() throws Exception {
final Processor testProcessor = new TestProcessor();
final Processor testProcessor2 = new TestProcessor2();
final Processor testProcessor3 = new TestProcessor3();
return new RouteBuilder() {
public void configure() {
errorHandler(new NoErrorHandlerBuilder());
from(CXF_RS_ENDPOINT_URI).process(testProcessor);
from(CXF_RS_ENDPOINT_URI2).process(testProcessor);
from(CXF_RS_ENDPOINT_URI3).process(testProcessor);
from(CXF_RS_ENDPOINT_URI4).process(testProcessor2);
from(CXF_RS_ENDPOINT_URI5).process(testProcessor3);
}
};
}
以及用于配置 endpoint… 的对应资源类
INFO:*Note about resource class*
默认情况下,JAX-RS 资源类仅*用于配置 JAX-RS 属性。在将信息路由到端点期间,将执行方法。相反,它负责执行所有处理的路由。
请注意,从 Camel 2.15 开始,仅提供一个接口,而不是默认模式的 no-op 服务实施类。
从 Camel 2.15 开始,如果启用了 performInvocation 选项,将首先调用服务实施,会在 Camel 交换上设置响应,并且路由执行将照常进行。这对于将现有的 JAX-RS 实现集成到 Camel 路由以及自定义处理器中的后处理 JAX-RS 响应时非常有用。
@Path("/customerservice/")
public interface CustomerServiceResource {
@GET
@Path("/customers/{id}/")
Customer getCustomer(@PathParam("id") String id);
@PUT
@Path("/customers/")
Response updateCustomer(Customer customer);
@Path("/{id}")
@PUT()
@Consumes({ "application/xml", "text/plain",
"application/json" })
@Produces({ "application/xml", "text/plain",
"application/json" })
Object invoke(@PathParam("id") String id,
String payload);
}