79.7. 消耗 REST Request - Default Binding Style

CXF JAXRS 前端 实施 JAX-RS (JSR-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:*注意资源类*

默认情况下,JAX-RS 资源类是 only* 用于配置 JAX-RS 属性。方法将 *not 在将消息路由到端点时执行。相反,路由负责执行所有处理。

请注意,从 Camel 2.15 开始,它还足以提供一个接口,而不能为默认模式实施类。

从 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);
}