搜索

18.10. 如何以 POJO 数据格式为 camel-cxf 端点准备消息

download PDF

camel-cxf 端点制作者基于 CXF 客户端 API。首先,您需要在消息标头中指定操作名称,然后将方法参数添加到列表中,并使用此参数列表初始化消息。响应消息的正文是一个 messageContentsList,您可以从该列表中获取结果。

如果您没有在消息标头中指定操作名称,CxfProducer 将尝试使用来自 CxfEndpointdefaultOperationName,如果在 CxfEndpoint 上没有设置defaultOperationName,它将从 Operation 列表中选择第一个 operationName。

如果要从消息正文中获取对象数组,您可以使用 message.getBody (Object[].class) 获取正文,如 CxfProducerRouterTest.testInvokingSimpleServerWithParams 所示:

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");
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.