78.7. REST 요청 사용 - 기본 바인딩 스타일

CXF JAXRS 프론트엔드JAX-RS(JSR-311) API 를 구현하므로 리소스 클래스를 REST 서비스로 내보낼 수 있습니다. 또한 CXF invocationr API 를 활용하여 REST 요청을 일반 Java 객체 메서드 호출로 전환합니다. Camel Restlet 구성 요소와 달리, 끝점 내에서 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);
        }
    };
}

또한 endpoint…​을 구성하는 데 사용되는 해당 리소스 클래스입니다.

INFO:* 리소스 클래스에 대한 참고*

기본적으로 JAX-RS 리소스 클래스는 JAX-RS 속성을 구성하는 데 사용되는 only*used입니다. 메서드는 끝점으로 메시지를 라우팅하는 동안 *not 가 실행됩니다. 대신, 모든 처리를 수행하는 경로의 책임이 있습니다.

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