78.8. camel-cxfrs 생산자를 통해 REST 서비스를 호출하는 방법


CXF JAXRS 프런트 엔드프록시 기반 클라이언트 API 를 구현합니다. 이 API는 프록시를 통해 원격 REST 서비스를 호출할 수 있습니다. camel-cxfrs 생산자는 이 프록시 API 를 기반으로 합니다. 메시지 헤더에 작업 이름을 지정하고 메시지 본문에 매개 변수를 준비하면 camel-cxfrs 생산자가 올바른 REST 요청을 생성합니다.

예를 들면 다음과 같습니다.

Exchange exchange = template.send("direct://proxy", new Processor() {
    public void process(Exchange exchange) throws Exception {
        exchange.setPattern(ExchangePattern.InOut);
        Message inMessage = exchange.getIn();
        // set the operation name
        inMessage.setHeader(CxfConstants.OPERATION_NAME, "getCustomer");
        // using the proxy client API
        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE);
        // set a customer header
        inMessage.setHeader("key", "value");
        // setup the accept content type
        inMessage.setHeader(Exchange.ACCEPT_CONTENT_TYPE, "application/json");
        // set the parameters , if you just have one parameter
        // camel will put this object into an Object[] itself
        inMessage.setBody("123");
    }
});

// get the response message
Customer response = (Customer) exchange.getOut().getBody();

assertNotNull("The response should not be null ", response);
assertEquals("Get a wrong customer id ", 123, response.getId());
assertEquals("Get a wrong customer name", "John", response.getName());
assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
assertEquals("Get a wrong header value", "value", exchange.getOut().getHeader("key"));
Copy to Clipboard Toggle word wrap

CXF JAXRS 프런트 엔드http 중심 클라이언트 API를 제공합니다. 또한 camel-cxfrs 생산자에서 이 API를 호출할 수도 있습니다. HTTP_PATH 및 HTTP_METHOD 를 지정하고 생산자가 URI 옵션 httpClientAPI 를 사용하거나 메시지 헤더 CxfConstants.CAMEL_CXF_RS_USING_HTTP_API 를 설정하여 http 중심 클라이언트 API를 사용하도록 해야 합니다. 응답 개체를 메시지 헤더 CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS 로 지정된 유형 클래스에 전환할 수 있습니다.

Exchange exchange = template.send("direct://http", new Processor() {
    public void process(Exchange exchange) throws Exception {
        exchange.setPattern(ExchangePattern.InOut)
        Message inMessage = exchange.getIn();
        // using the http central client API
        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.TRUE);
        // set the Http method
        inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
        // set the relative path
        inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/123");
        // Specify the response class , cxfrs will use InputStream as the response object type
        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
        // set a customer header
        inMessage.setHeader("key", "value");
        // since we use the Get method, so we don't need to set the message body
        inMessage.setBody(null);
    }
});
Copy to Clipboard Toggle word wrap

Camel 2.1에서 CXFRS http 중심 클라이언트의 cxfrs URI에서 쿼리 매개변수를 지정할 수도 있습니다.

Exchange exchange = template.send("cxfrs://http://localhost:9003/testQuery?httpClientAPI=true&q1=12&q2=13"
Copy to Clipboard Toggle word wrap

동적 라우팅을 지원하기 위해 CxfConstants.CAMXF_RS_QUERY_MAP 헤더를 사용하여 URI의 쿼리 매개 변수를 재정의하여 해당 URI에 대한 매개변수 맵을 설정할 수 있습니다.

Map<String, String> queryMap = new LinkedHashMap<>();
queryMap.put("q1", "new");
queryMap.put("q2", "world");
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_QUERY_MAP, queryMap);
Copy to Clipboard Toggle word wrap
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat