Apache Camel:ProducerTemplate不会解组响应

时间:2016-05-24 09:27:01

标签: java apache-camel

骆驼版:2.15.6

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

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

});

我将联系人视为空。

当我尝试像对象一样将它作为对象:

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

NS))

为什么ProducerTemplate无法解组对指定对象的响应? 怎么能实现这一目标?

修改

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

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

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

2 个答案:

答案 0 :(得分:1)

尝试创建这样的路线:

//org.apache.camel.component.jackson.JacksonDataFormat

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

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

解组后你的身体应该有Contant个物体。

依赖性来自:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-jackson</artifactId>
    <version>2.15.6</version>
</dependency>

答案 1 :(得分:0)

我的答案更多的是拉法尔的组合,以展示如何将他的代码与您的解决方案联系起来以获得理想的结果。感谢Rafal在此示例中设置子路径。

假设:您已经有其他API已经可用

您的新代码:

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);
相关问题