【问题标题】:How to invoke a simple webservice with camel?如何用骆驼调用一个简单的网络服务?
【发布时间】:2015-09-30 10:23:44
【问题描述】:

这是我的路线:

<route>
<from uri="timer:timerName?period=2000"/>
<to uri="ahc:http://www.google.com/search?q=Camel"/>
<log message="${property.CamelHttpResponseCode}"/>
</route>

我想获取响应代码状态,但我收到了这个错误

Exchange[Message: [Body is null]]

我该如何解决这个问题?

【问题讨论】:

    标签: web-services apache-camel blueprint


    【解决方案1】:

    我想阅读documentation 中的“获取响应代码”段落会有所帮助:

    Exchange exchange = template.send("ahc:http://www.google.com/search", new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setHeader(Exchange.HTTP_QUERY, constant("hl=en&q=activemq"));
         }
    });
    Message out = exchange.getOut();
    int responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class);
    

    【讨论】:

    • 感谢您的回答!但我想在 blueprint.xml 中的路线内而不是使用 POJO
    • AHC 代表异步 Http 客户端。在 ahc 调用之后,您无法在您的路线中获得结果。或者你必须禁用异步。
    【解决方案2】:

    对于这个简单的案例,您可以使用骆驼码头。否则,还有用于调用和公开 HTTP 服务的附加组件,例如 Camel-Restlet。 http://camel.apache.org/jetty.html

    <route>
        <from uri="direct:start"/>
        <to uri="http://www.google.com/search?q=Camel"/>
    <route>
    

    编辑:

    仅出于测试目的,您可以这样做:

    <route>
        <from uri="jetty://localhost:5050/test"/>
        <to uri="jetty://http://www.google.com?q=Camel"/>
    <route>
    

    然后在 chrome 或 fiddler 中使用 postman 之类的工具并调用您的 localhost 服务。

    【讨论】:

    • 我这样做了,但使用直接组件,路由不启动:我无法读取结果我收到此消息:Apache Camel 2.15.1.redhat-620133 (CamelContext: camel-1) 在 0.359 秒内启动没有别的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 2013-11-20
    • 1970-01-01
    相关资源
    最近更新 更多