使用骆驼生产者模板和http时未获得http响应正文 [英] Not getting http response body when using camel producertemplate and http

查看:104
本文介绍了使用骆驼生产者模板和http时未获得http响应正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在 http://camel上指定的生产者模板来调用我的简单GET REST服务调用. 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的方式有关,因为如果我删除它并从 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆