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

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

问题描述

骆驼版本:2.15.6

我使用 ProducerTemplate 发送一个 http 请求并得到这样的响应.

from("direct:getContact").process(新处理器(){@覆盖public void process(Exchange exchange) 抛出异常 {CamelContext 上下文 = exchange.getContext();ProducerTemplate producerTemplate = context.createProducerTemplate();Contact contact = producerTemplate.requestBodyAndHeaders("http://localhost:8080/api/contact/2345",null,标题,Contact.class);logger.info("联系人是:" + new ObjectMapper().writeValueAsString(contact));exchange.getOut().setBody(contact);});

我将联系人设为空.

当我尝试将它作为这样的对象时:

Object contact = producerTemplate.requestBodyAndHeaders("http://localhost:8080/api/contact/2345",空,标题);logger.info("联系人是:" + new ObjectMapper().writeValueAsString(contact));

<块引用>

com.fasterxml.jackson.databind.JsonMappingException:没有序列化程序为班级找到org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream并且没有发现创建 BeanSerializer 的属性(以避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEA

NS) )

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

编辑

我观察到的修复如下:如果我首先将输出作为字符串然后反序列化它,它就可以工作.

String responseString = producerTemplate.requestBodyAndHeaders("http://localhost:8080/api/contact/2345",null,标题,String.class);Contact contact = new ObjectMapper().readValue(responseString, Contact.class);

解决方案

我的回答更像是 Rafal 的组合,以展示如何将他的代码重新绑定到您的解决方案中以获得所需的结果.感谢 Rafal 在此示例中设置子路由.

假设:您已经安装了其余的 API 并可用

您的新代码:

from("direct:getContact").process(新处理器(){@覆盖public void process(Exchange exchange) 抛出异常 {CamelContext 上下文 = exchange.getContext();ProducerTemplate producerTemplate = context.createProducerTemplate();//调用另一个路由而不是rest端点未来<联系方式>联系 = producerTemplate.requestBodyAndHeaders("direct:RetrieveContactRoute",null,标题,Contact.class);logger.info("联系人是:" + new ObjectMapper().writeValueAsString(contact.get()));//设置入体而不是出体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);

Camel version: 2.15.6

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: 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) )

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

Edit

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

解决方案

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.

Assumptions: You have the rest API up and available already

Your new Code:

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

A separate Route

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天全站免登陆