27.10. 消耗 REST 请求 - 默认绑定 Style
CXF JAXRS 前端 实施 JAX-RS (concurrency-311) API,因此我们可以将资源类导出为 REST 服务。我们利用 CXF Invoker API 将 REST 请求转换为普通的 Java 对象方法调用。不需要在端点中指定 URI 模板。CXF 根据 JSR-311 规范,负责 REST 请求 URI 到资源类方法映射。您需要在 Camel 中做的只是将此方法请求委派给正确的处理器或端点。
CXFRS 路由示例
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);
}
};
}
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);
}
};
}
对应的资源类则用于配置端点。
- 注意
- 默认情况下,JAX-RS 资源类 仅用于 配置 JAX-RS 属性。在将消息路由到端点时不会执行方法。相反,负责执行所有处理。
对于默认模式,只提供一个接口,而不是 no-op 服务实现类。
如果启用了 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);
}
@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);
}