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

时间:2015-01-15 11:29:21

标签: java json spring serialization spring-integration

我们收到以下错误:

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

附加http:outbound-gateway的{​​{1}}引发了这种情况,如下所示:

MappingJackson2HttpMessageConverter

被序列化的Out对象注释为Jackson序列化:

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

相信这与Spring Integration 2.2一起使用,并且在迁移到3.0时开始失败。

我突然想到我们试图序列化为public class FileRecord { @JsonProperty private final String id; @JsonProperty private final String path; ... } 。我希望application/x-java-serialized-object在这里。也许需要application/json?如果是这样,我想了解为什么需要表达这一点。我的header-enricher不应该知道吗?

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

...

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

......你应该很好。