简体   繁体   中英

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

We are receiving the following error:

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

This is being thrown by an http:outbound-gateway with a MappingJackson2HttpMessageConverter attached, like so:

<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 object being serialized is annotated for Jackson serialization:

public class FileRecord {

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

    ...

}

I believe this was working with Spring Integration 2.2 and started to fail with a migration to 3.0.

It jumps out at me as odd that we are trying to serialize as application/x-java-serialized-object . I'd expect application/json here. Perhaps a header-enricher is called for? If so, I'd like to understand why exactly this needs to expressed. Shouldn't my jsonMessageConverter know this?

I'm not sure what the true cause or fix is, but I found a different approach which does the trick.

First, I removed the MappingJackson2HttpMessageConverter bean entirely.

I then added an extra transformer to explicitly convert my POJO to 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"/>

For the outbound-gateway , I just needed to remove the message-converters as my payload is now 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"/>

??

You are replacing the default supported (json) media types with this...

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

...the constructor sets them up properly...

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

Just remove...

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

...and you should be good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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