繁体   English   中英

使用Apache Camel打个电话

[英]Making a rest call with Apache Camel

嗨,我想调用URL为

http://ex.abc.com/orders/resources/{var1}/{var2}/details?orderNumber=XXXXX

其中var1和var2是动态值。 根据输入,它们将改变。 我还想设置2个标头,例如key1:value1,key2:value2。

如何使用给定的标头对给定的URL进行剩余调用,然后使用Apache Camel查看响应? (响应将始终为JSON)。

您可以在路由块中使用动态uri。 请参阅http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html。请注意,这可以在from()和to()中完成。

例:
[来自(先前的应用程序端点)
-> [To(使用来自交易所的动态值执行休息)]
-> [至(处理返回的json)]

如果要打个电话,可以使用CXFRS组件。 在页面的最底部,您将看到一个处理器的示例,该示例将消息设置为休息呼叫。 关于您的问题,您可以使用

inMessage.setHeader(Exchange.HTTP_PATH, "/resources/{var1}/{var2}/details?orderNumber=XXXXX");

设置您可能需要的任何路径参数。

请尝试使用骆驼servlet。

<from uri =“ servlet:/// orders / resources / {$ var1} / {$ var2} / details?orderNumber = XXXXX” />

在web.xml中,<url-pattern> / * </ url-pattern>

参考: http : //camel.apache.org/servlet.html

在下面的示例中,您可以使用Apache Camel查找Rest调用。

package camelinaction;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import org.apache.camel.spring.boot.FatJarRouter;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class OrderRoute extends FatJarRouter {

    @Bean(name = "jsonProvider")
    public JacksonJsonProvider jsonProvider() {
        return new JacksonJsonProvider();
    }

    @Override
    public void configure() throws Exception {
        // use CXF-RS to setup the REST web service using the resource class
        // and use the simple binding style which is recommended to use
        from("cxfrs:http://localhost:8080?resourceClasses=camelinaction.RestOrderService&bindingStyle=SimpleConsumer&providers=#jsonProvider")
            // call the route based on the operation invoked on the REST web service
            .toD("direct:${header.operationName}");

        // routes that implement the REST services
        from("direct:createOrder")
            .bean("orderService", "createOrder");

        from("direct:getOrder")
            .bean("orderService", "getOrder(${header.id})");

        from("direct:updateOrder")
            .bean("orderService", "updateOrder");

        from("direct:cancelOrder")
            .bean("orderService", "cancelOrder(${header.id})");
    }
}

源代码链接:

https://github.com/camelinaction/camelinaction2/tree/master/chapter10/camel-cxf-rest-spring-boot

我强烈建议您引用camelinaction2,因为它涵盖了许多高级主题,并且由@Claus Ibsen撰写。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM