如何在Java DSL中从Camel调用REST调用(带有JSON主体的POST)

杰特凡

在IBM WebSphere上运行的Web项目的ServletContextListener中具有以下工作骆驼流设置,将传入的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");
    }
}

我必须扩展流程以将JSON发布到REST服务/消费者。(其余资源已经过测试并且可以正常工作。)

搜索(web)文档不会给我提供我可以使用的良好/完整的Java DSL示例。根据我的发现,我认为这应该类似于添加以下端点:

.to("cxfrs://http://localhost:9080/WebContext/TestResource") 

但这是行不通的,而且我不明白如何将转换后的JSON设置为主体并将其设为POST请求。也没有例外。

如何在此流程中将REST调用添加为带有JSON正文的POST?

在IBM WebSphere v8.5.5,IBM jdk 1.7x,Camel 2.11.2中运行

以下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.jackson.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

谢谢。

江em

如果您只想将JSON消息发布到REST服务,则不需要使用camel-cxfrs组件,因为您已经具有请求消息主体,因此只需要使用camel-http端点发送请求即可。

所以路线可能是

from("jms:queue:TestQueue?concurrentConsumers=1")
                    .marshal(xmlJsonFormat)
                    .to("http://localhost:9080/WebContext/TestResource");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在蓝图中从Camel调用REST调用(带有JSON主体的POST)

如何使用Java DSL在Apache Camel中调用带有参数的方法

如何在Springboot中将具有JSON主体的POST API调用转换为Java类?

如何在我的角度应用程序中调用和使用spring REST POST API,该应用程序返回没有主体的字符串?

如何在react post方法调用中传递带有表示数字数据类型的值的json属性

如何在C#中调用带有内容和标题的Rest API?

Java中通过JSON调用带有参数的方法

如何在Java中调用图形类?

如何在Java中多次调用launch()

如何在Jsp中调用Java类

如何在Java程序中调用shellscript?

如何在Java中调用InputVerifier?

如何在JAVA中捕获系统调用?

如何在XHTML中调用Java方法

如何在Java中调用重载方法

如何在JavaScript中调用Java方法

如何在Java中“自动调用”方法

如何在Java中调用冒泡排序

如何在Java中调用Python函数

如何在API调用中反序列化带有多个标签的大型JSON

如何在Assembly中调用带有大量arg的函数

如何在Django模板中调用带有参数的函数?

如何在带有参数的函数中调用变量函数?

如何在Blazor中调用带有参数的函数?

如何在Makefile中的for循环主体中调用函数

如何使用带有操作名称的Spring Integration Java DSL调用soap Webservice

如何在Angular 10中调用主体onload JavaScript函数

如何在带有 Java DSL 的路由中使用 apache Camel 在 try catch 块中使用选择

如何在Java中修改HttpServletRequest主体?