79.8. camel-cxfrs プロデューサーを介して REST サービスを呼び出す方法


CXF JAXRS フロントエンド は、プロキシーベースのクライアント API を実装します。この API を使用すると、プロキシーを介してリモート REST サービスを呼び出すことができます。camel-cxfrs producer は、この プロキシー API に基づいています。メッセージヘッダーで操作名を指定し、メッセージボディーでパラメーターを準備するだけで、camel-cxfrs producer が適切な 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"));

CXF JAXRS フロントエンド は、http 中心のクライアント API も提供します。この API を camel-cxfrs プロデューサーから呼び出すこともできます。HTTP_PATHHTTP_METHOD を指定し、URI オプション httpClientAPI を使用するか、メッセージヘッダー CxfConstants.CAMEL_CXF_RS_USING_HTTP_API を設定して、producer が 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);
    }
});

Camel 2.1 から、CXFRS http セントリッククライアントの cxfrs URI からクエリーパラメーターを指定することもサポートしています。

Exchange exchange = template.send("cxfrs://http://localhost:9003/testQuery?httpClientAPI=true&q1=12&q2=13"

動的ルーティングをサポートするには、CxfConstants.CAMEL_CXF_RS_QUERY_MAP ヘッダーを使用して URI のクエリーパラメーターをオーバーライドし、そのパラメーターマップを設定します。

Map<String, String> queryMap = new LinkedHashMap<>();
queryMap.put("q1", "new");
queryMap.put("q2", "world");
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_QUERY_MAP, queryMap);
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2026 Red Hat
トップに戻る