gpt4 book ai didi

java - Spring 集成 http :outbound-gateway "no suitable HttpMessageConverter"

转载 作者:行者123 更新时间:2023-11-30 03:35:38 25 4
gpt4 key购买 nike

我们收到以下错误:

org.springframework.web.client.RestClientException: Could not write request:
no suitable HttpMessageConverter found for
request type [com.company.FileRecord] and
content type [application/x-java-serialized-object]
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:770)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:549)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:527)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:472)
...

这是由附加了 MappingJackson2HttpMessageConverterhttp:outbound-gateway 抛出的,如下所示:

<bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes"
value="application/x-java-serialized-object"/>
</bean>

<int:transformer input-channel="transformationChannel"
output-channel="registrationQueue"
ref="fileTransformer"/>

<int:channel id="registrationQueue"/>

<int-http:outbound-gateway id="gateway"
request-channel="registrationQueue"
message-converters="jsonMessageConverter"
url-expression="@urlGenerator.resolve()"
http-method="POST"
expected-response-type="javax.ws.rs.core.Response"
reply-channel="nullChannel"
error-handler="httpResponseErrorHandler"/>

正在序列化的输出对象被注释为 Jackson 序列化:

public class FileRecord {

@JsonProperty
private final String id;
@JsonProperty
private final String path;

...

}

相信这适用于 Spring Integration 2.2,但在迁移到 3.0 时开始失败。

让我感到奇怪的是,我们试图序列化为 application/x-java-serialized-object。我希望这里有 application/json 。也许需要一个header-enricher?如果是这样,我想了解为什么需要表达这一点。我的 jsonMessageConverter 不应该知道这一点吗?

最佳答案

我不确定真正的原因或修复是什么,但我发现了一种不同的方法可以解决问题。

首先,我完全删除了 MappingJackson2HttpMessageConverter bean。

然后我添加了一个额外的转换器来显式地将 POJO 转换为 JSON:

<int:transformer input-channel="objectTransformationChannel"
output-channel="jsonTransformationChannel"
ref="fileTransformer"/>
<int:channel id="jsonTransformationChannel"/>
<int:object-to-json-transformer input-channel="jsonTransformationChannel"
output-channel="registrationQueue"/>
<int:channel id="registrationQueue"/>

对于出站网关,我只需要删除消息转换器,因为我的负载现在是 JSON。

<int-http:outbound-gateway id="gateway"
request-channel="registrationQueue"
url-expression="@urlGenerator.resolve()"
http-method="POST"
expected-response-type="javax.ws.rs.core.Response"
reply-channel="nullChannel"
error-handler="httpResponseErrorHandler"/>

关于java - Spring 集成 http :outbound-gateway "no suitable HttpMessageConverter",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27962396/

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