Not getting http response body when using camel producertemplate and http

人走茶凉 提交于 2019-12-11 09:37:30

问题


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:

Response from http template is null

status header is null


回答1:


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));
    }

}

yields

Response from http template is org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream@26659db7
status header is 200



回答2:


The send method is an in only pattern, you may have more luck using the request method (see the official documentation)

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");
        }
});



回答3:


You can access your response via exchange.getIn().getBody(String.class)




回答4:


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");
        }
});

How to call post service if I have body and headers.



来源:https://stackoverflow.com/questions/12306211/not-getting-http-response-body-when-using-camel-producertemplate-and-http

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!