使用骆驼生产者模板和 http 时没有得到 http 响应正文 [英] Not getting http response body when using camel producertemplate and http
问题描述
我正在尝试使用 http://camel 上指定的生产者模板调用我的简单 GET 休息服务调用.apache.org/http.html.我在这里以 google.com 为例.这是来自未在任何容器上运行的独立客户端.我在这里没有做什么?
I am trying to invoke my simple GET rest service call using producertemplate as specified on http://camel.apache.org/http.html. I have used google.com as an example here. This is from a standalone client not running on any container. What am i not doing right here?
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));
我没有得到任何回应.输出为:
I dont get any response. The output is:
来自 http 模板的响应为空
Response from http template is null
状态头为空
推荐答案
这与您从 Spring 创建camelContext 的方式有关,因为如果我删除它并将CamelContext 从DefaultCamelContext 我没有看到问题:
It has to do with the way you are creating the camelContext from Spring because if I remove that and get the CamelContext off the DefaultCamelContext I don't see an issue:
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
这篇关于使用骆驼生产者模板和 http 时没有得到 http 响应正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!