【问题标题】:Spring Integration http:outbound-gateway "no suitable HttpMessageConverter"Spring 集成 http:outbound-gateway “没有合适的 HttpMessageConverter”
【发布时间】:2015-01-15 11:29:21
【问题描述】:

我们收到以下错误:

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

被序列化的 Out 对象为 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不应该知道吗?

【问题讨论】:

    标签: java json spring serialization spring-integration


    【解决方案1】:

    我不确定真正的原因或解决方法是什么,但我找到了一种不同的方法来解决问题。

    首先,我完全删除了 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"/>
    

    对于outbound-gateway,我只需要删除message-converters,因为我的有效负载现在是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"/>
    

    【讨论】:

    • 是的,这也可以,但是过滤掉 content-type 标头应该可以使用您以前的配置。
    【解决方案2】:

    ??

    您正在用此替换默认支持的 (json) 媒体类型...

    <property name="supportedMediaTypes"
              value="application/x-java-serialized-object"/>
    

    ...构造函数正确设置它们...

    public MappingJackson2HttpMessageConverter() {
        super(new MediaType("application", "json", DEFAULT_CHARSET),
                new MediaType("application", "*+json", DEFAULT_CHARSET));
    }
    

    只需删除...

    <property name="supportedMediaTypes"
              value="application/x-java-serialized-object"/>
    

    ...你应该很好。

    【讨论】:

    • 但是错误消息“content type [application/x-java-serialized-object]”不是告诉我我的内容属于那种类型吗?
    • 是的,你是对的,对不起;没有注意到异常,但是配置肯定是错误的。这个流程的上游是什么?是这样设置内容类型标题的吗?也许在某个地方为您的流程发布调试日志。如果它是由上游设置的,则需要一个标头过滤器来删除它。
    • @GaryRussell 如果我理解您的消息,并且根据我的期望,如果没有明确设置内容类型标头,我希望 HTTP 出站网关确定要用作 @987654325 的适当内容类型@ 确实(通过查询配置的MessageConverters)。然而,Spring Integration 4.3 中的HttpRequestExecutingMessageHandler.resolveContentType(Object) 正是这样做的:将内容类型显式设置为application/x-java-serialized-object,无论是不是byte[]SourceMap。所以我的 POJO 有效负载产生了上述异常。这是预期的吗?
    • 请不要评论 5 年前的答案;问一个新问题并显示您的代码/配置。但是,从表面上看,这听起来像是一个错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 2018-01-22
    • 1970-01-01
    相关资源
    最近更新 更多