17.10. 如何以 POJO 数据格式为 camel-cxf 端点准备消息
camel-cxf
端点制作者基于 CXF 客户端 API。首先,您需要在消息标头中指定操作名称,然后将方法参数添加到列表中,并使用此参数列表初始化消息。响应消息的正文是 messageContentsList,您可以从该列表中获取结果。
如果您没有在消息标头中指定操作名称,CxfProducer
将尝试从 CxfEndpoint 使用 defaultOperationName
,如果没有在
上设置 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");