gpt4 book ai didi

java - Apache Camel : ProducerTemplate not unmarshalling the response

转载 作者:行者123 更新时间:2023-11-30 07:21:57 25 4
gpt4 key购买 nike

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: 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 无法解码对指定对象的响应?如何才能实现这一目标?

编辑

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

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

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

最佳答案

尝试像这样创建您的路线:

//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>

关于java - Apache Camel : ProducerTemplate not unmarshalling the response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37409460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com