繁体   English   中英

Spring Integration http:outbound-gateway“没有合适的HttpMessageConverter”

[英]Spring Integration http:outbound-gateway “no suitable HttpMessageConverter”

我们收到以下错误:

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不应该知道吗?

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

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

??

您正在用此替换默认的受支持(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"/>

...你应该很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM