1

我正在尝试使用http://camel.apache.org/http.html上指定的 producertemplate 调用我的简单 GET 休息服务调用。我在这里以 google.com 为例。这是来自未在任何容器上运行的独立客户端。我没有在这里做什么?

SpringCamelContext camelcontext = (SpringCamelContext) springContext.getBean("camelcontextbean");

ProducerTemplate template = camelcontext.createProducerTemplate();
camelcontext.start();
Exchange exchange = template.send("http://www.google.com/search", new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setHeader(Exchange.HTTP_QUERY, "hl=en&q=activemq");
            }
   });

   Message out = exchange.getOut();
System.out.println("Response from http template is "+exchange.getOut().getBody());
   System.out.println("status header is "+out.getHeader(Exchange.HTTP_RESPONSE_CODE));

我没有得到任何回应。输出是:

来自 http 模板的响应为空

状态标头为空

4

4 回答 4

5

它与您从 Spring 创建 camelContext 的方式有关,因为如果我删除它并从 DefaultCamelContext 中获取CamelContext不到问题:

import org.apache.camel.*;
import org.apache.camel.impl.DefaultCamelContext;

public class Main {

    public static void main(String ... args){
        CamelContext camelContext = new DefaultCamelContext();
        ProducerTemplate template = camelContext.createProducerTemplate();

        Exchange exchange = template.send("http://www.google.com/search", new Processor() {
            public void process(Exchange exchange) throws Exception {
                exchange.getIn().setHeader(Exchange.HTTP_QUERY, "hl=en&q=activemq");
            }
        });

        Message out = exchange.getOut();
        System.out.println("Response from http template is "+exchange.getOut().getBody());
        System.out.println("status header is "+out.getHeader(Exchange.HTTP_RESPONSE_CODE));
    }

}

产量

Response from http template is org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream@26659db7
status header is 200
于 2012-09-06T22:41:14.397 回答
2

send 方法是一种 in only 模式,使用 request 方法可能会更幸运(参见官方文档

Exchange exchange = template.request("http://www.google.com/search", new Processor() {
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(Exchange.HTTP_QUERY, "hl=en&q=activemq");
        }
});
于 2012-09-06T20:24:10.383 回答
1

您可以通过以下方式访问您的回复exchange.getIn().getBody(String.class)

于 2015-02-02T11:48:18.023 回答
0
Exchange exchange = template.request("http://www.google.com/search", new Processor() {
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(Exchange.HTTP_QUERY, "hl=en&q=activemq");
        }
});

如果我有正文和标题,如何调用邮政服务。

于 2017-01-05T12:14:01.483 回答