如何在 Java DSL 中从 Camel 调用 REST 调用(POST with JSON body)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19183296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to invoke a REST call (POST with JSON body) from Camel in Java DSL
提问by JStefan
Having the following working Camel flow setup in a ServletContextListener in a Web project running on IBM WebSphere, an incoming XML is converted to JSON and printed to the System.out and printed to the report.txt. So far so good.
在 IBM WebSphere 上运行的 Web 项目中的 ServletContextListener 中设置以下工作 Camel 流,传入的 XML 被转换为 JSON 并打印到 System.out 并打印到 report.txt。到现在为止还挺好。
@WebListener
public class SetupCamel implements ServletContextListener {
private CamelContext camelContext;
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("SetupCamel:contextInitialized - enter");
try {
Context ctx = new InitialContext();
QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("jms/TestConnectionFactory");
camelContext = new DefaultCamelContext();
JmsConfiguration jmsConfiguration = new JmsConfiguration(qcf);
JmsComponent jmsComponent = new JmsComponent(jmsConfiguration);
camelContext.addComponent("jms", jmsComponent);
final XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
xmlJsonFormat.setEncoding("UTF-8");
xmlJsonFormat.setForceTopLevelObject(false);
xmlJsonFormat.setTrimSpaces(true);
xmlJsonFormat.setRootName("newRoot");
xmlJsonFormat.setSkipNamespaces(true);
xmlJsonFormat.setRemoveNamespacePrefixes(true);
camelContext.addRoutes(new RouteBuilder() {
public void configure() {
onException(Exception.class)
.to("log:GeneralError?level=ERROR")
.end();
from("jms:queue:TestQueue?concurrentConsumers=1")
.marshal(xmlJsonFormat)
.to("file:/tmp/messages?fileName=report.txt&fileExist=Append")
.to("stream:out")
;
}
});
camelContext.start();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("SetupCamel:contextInitialized - leaving");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("SetupCamel:contextDestroyed - enter");
try {
if (camelContext != null) {
camelContext.stop();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("SetupCamel:contextDestroyed - leaving");
}
}
I have to extend the flow to POST the JSON to a REST service/consumer. (The Rest resource is tested and works..)
我必须扩展流程以将 JSON 发布到 REST 服务/消费者。(Rest 资源已经过测试并且可以正常工作..)
Searching (web)documentation doesn't give me a good/complete Java DSL example that I'm able to use. Based upon what I did find, I think it should be something similar to adding a endpoint like:
搜索 (web) 文档并没有给我一个我可以使用的好的/完整的 Java DSL 示例。根据我的发现,我认为它应该类似于添加一个端点,例如:
.to("cxfrs://http://localhost:9080/WebContext/TestResource")
But this doesn't work and I don't understand how to set the converted JSON as body and make it a POST request. There is also no exception printed.
但这不起作用,我不明白如何将转换后的 JSON 设置为正文并使其成为 POST 请求。也没有例外打印。
How can I add the REST call as a POST with JSON body in this flow ?
如何在此流程中将 REST 调用添加为带有 JSON 正文的 POST?
Running in IBM WebSphere v8.5.5, IBM jdk 1.7x, Camel 2.11.2
在 IBM WebSphere v8.5.5、IBM jdk 1.7x、Camel 2.11.2 中运行
The following jar files are in the WEB-INF/lib classpath:
以下 jar 文件位于 WEB-INF/lib 类路径中:
camel-core-2.11.2.jar
camel-cxf-2.11.2.jar
camel-cxf-transport-2.11.2.jar
camel-jms-2.11.2.jar
camel-servletlistener-2.11.2.jar
camel-spring-2.11.2.jar
camel-stream-2.11.2.jar
camel-xmljson-2.11.2.jar
com.ibm.ws.prereq.Hymanson.jar
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
cxf-api-2.7.6.jar
cxf-rt-frontend-jaxrs-2.7.6.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar
slf4j-api-1.7.5.jar
spring-beans-3.1.4.RELEASE.jar
spring-context-3.1.4.RELEASE.jar
spring-core-3.1.4.RELEASE.jar
spring-jms-3.1.4.RELEASE.jar
spring-tx-3.1.4.RELEASE.jar
xom-1.2.5.jar
Thanks.
谢谢。
回答by Willem Jiang
If you just want to post the JSON message to the REST service, you don't need to use camel-cxfrs component, as you already has the request message body, you just need to use camel-http endpoint to send the request.
如果您只是想将 JSON 消息发布到 REST 服务,则不需要使用 camel-cxfrs 组件,因为您已经有了请求消息正文,您只需要使用 camel-http 端点发送请求即可。
So the route could be
所以路线可以是
from("jms:queue:TestQueue?concurrentConsumers=1")
.marshal(xmlJsonFormat)
.to("http://localhost:9080/WebContext/TestResource");
回答by jmathewt
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
String body = (String) exchange.getIn().getBody();
logger.info(body);
exchange.getIn().setHeader(Exchange.HTTP_METHOD, constant(HttpMethod.POST));
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_JSON);
exchange.getIn().setHeader("Authorization", "Bearer " + getToken());
HttpServletRequest request = exchange.getIn().getBody(HttpServletRequest.class);
exchange.getIn().setHeader(Exchange.HTTP_SERVLET_REQUEST, request); // POST body is set here
}
})
.to(apiToCall);