Apache Camel:ProducerTemplate不解组响应 [英] Apache Camel: ProducerTemplate not unmarshalling the response

查看:354
本文介绍了Apache Camel:ProducerTemplate不解组响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

骆驼版本:2.15.6

Camel version: 2.15.6

我使用ProducerTemplate发送http请求并获得了这样的响应.

I used the ProducerTemplate to send a http request and get the response like this.

from("direct:getContact")
.process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        CamelContext context = exchange.getContext();
                        ProducerTemplate producerTemplate = context.createProducerTemplate();
Contact contact = producerTemplate.requestBodyAndHeaders( 
                                "http://localhost:8080/api/contact/2345", 
                                null, headers, Contact.class); 

logger.info("Contact is: " + new ObjectMapper().writeValueAsString(contact)); 

exchange.getOut().setBody(contact);

});

我得到的联系人为空.

I get the contact as null.

当我尝试将其作为对象获取时:

When I try to get it as Object like this:

Object contact = producerTemplate.requestBodyAndHeaders( 
                                "http://localhost:8080/api/contact/2345", 
                                null, headers); 


logger.info("Contact is: " + new ObjectMapper().writeValueAsString(contact)); 

com.fasterxml.jackson.databind.JsonMappingException:没有序列化程序 被发现上课 org.apache.camel.converter.stream.CachedOutputStream $ WrappedInputStream 并没有发现创建BeanSerializer的属性(避免 例外,请禁用SerializationFeature.FAIL_ON_EMPTY_BEA

com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEA

NS))

为什么ProducerTemplate无法解组对指定对象的响应? 如何做到这一点?

Why is ProducerTemplate not able to unmarshall the response to the specified object? How can this be achieved?

修改

我观察到的修复方法如下: 如果我首先以字符串形式获取输出,然后反序列化它,那么它将起作用.

The Fix I observed is as follows: If I first get the output as string and then deserialize it, it works.

String responseString = producerTemplate.requestBodyAndHeaders( 
                                    "http://localhost:8080/api/contact/2345", 
                                    null, headers, String.class); 

Contact contact = new ObjectMapper().readValue(responseString, Contact.class);

推荐答案

我的答案"更多是Rafal的组合,以显示您如何将其代码重新绑定到解决方案中以获得期望的结果.感谢Rafal在此示例中设置了子路线.

My Answer is more of an combination of Rafal's to show how you can tie his code back into your solution to get the desired result. Thanks Rafal for setting up the sub route in this sample.

假设:您已经建立了其余的API并可以使用

Assumptions: You have the rest API up and available already

您的新代码:

from("direct:getContact")
    .process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            CamelContext context = exchange.getContext();
            ProducerTemplate producerTemplate = context.createProducerTemplate();
            //Call another route not the rest endpoint
            Future<Contact> contact = producerTemplate.requestBodyAndHeaders( 
                "direct:RetrieveContactRoute", 
                 null, headers, Contact.class); 

             logger.info("Contact is: " + new ObjectMapper().writeValueAsString(contact.get())); 

             //Set the In Body not the Out Body 
             exchange.getIn().setBody(contact.get());
    }); 

一条单独的路线

JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
jacksonDataFormat.setUnmarshalType(Contact.class);

from("direct:RetrieveContactRoute")
    .to("http://localhost:8080/api/contact/2345")
    .unmarshal(jacksonDataFormat);

这篇关于Apache Camel:ProducerTemplate不解组响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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