簡體   English   中英

如何從Java DSL中的Camel調用REST調用(帶JSON主體的POST)

[英]How to invoke a REST call (POST with JSON body) from Camel in Java DSL

在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");
    }
}

我必須擴展流程以將JSON POST到REST服務/使用者。 (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

謝謝。

如果您只想將JSON消息發布到REST服務,則不需要使用camel-cxfrs組件,因為您已經擁有請求消息正文,您只需使用camel-http端點發送請求即可。

所以路線可能是

from("jms:queue:TestQueue?concurrentConsumers=1")
                    .marshal(xmlJsonFormat)
                    .to("http://localhost:9080/WebContext/TestResource");
.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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM