17.10. POJO データ形式で camel-cxf エンドポイントのメッセージを準備する方法
camel-cxf エンドポイントプロデューサーは、CXF client API に基づいています。まず、メッセージヘッダーでオペレーション名を指定し、次にメソッドパラメーターをリストに追加し、このパラメーターリストでメッセージを初期化する必要があります。応答メッセージの本文は messageContentsList であり、そのリストから結果を取得できます。
メッセージヘッダーで操作名を指定しない場合、CxfProducer は CxfEndpoint から defaultOperationName を使用しようとします。CxfEndpoint に defaultOperationName が設定されていない場合、操作リストから最初の operationName を取得します。
メッセージ本文からオブジェクト配列を取得する場合は、CxfProducerRouterTest.testInvokingSimpleServerWithParams に示すように、message.getBody (Object.class) を使用して本文を取得できます。
Exchange senderExchange = new DefaultExchange(context, ExchangePattern.InOut);
final List<String> params = new ArrayList<>();
// Prepare the request message for the camel-cxf procedure
params.add(TEST_MESSAGE);
senderExchange.getIn().setBody(params);
senderExchange.getIn().setHeader(CxfConstants.OPERATION_NAME, ECHO_OPERATION);
Exchange exchange = template.send("direct:EndpointA", senderExchange);
org.apache.camel.Message out = exchange.getMessage();
// The response message's body is an MessageContentsList which first element is the return value of the operation,
// If there are some holder parameters, the holder parameter will be filled in the reset of List.
// The result will be extract from the MessageContentsList with the String class type
MessageContentsList result = (MessageContentsList) out.getBody();
LOG.info("Received output text: " + result.get(0));
Map<String, Object> responseContext = CastUtils.cast((Map<?, ?>) out.getHeader(Client.RESPONSE_CONTEXT));
assertNotNull(responseContext);
assertEquals("UTF-8", responseContext.get(org.apache.cxf.message.Message.ENCODING),
"We should get the response context here");
assertEquals("echo " + TEST_MESSAGE, result.get(0), "Reply body on Camel is wrong");