Search code examples
javaspringspring-integrationspring-integration-dsl

Is any additional attributes required to force the java type in spring integration


We are using spring-integation (xml based configuration), In which we are performing below steps

  1. Convert the payload (java-object) to json
  2. Make the rest api call
  3. Convert back to java-object
<int:object-to-json-transformer content-type="test.Request" input-channel="objectToJsonChannel" output-channel="apiChannel"/>
       <int-http:outbound-gateway id="apiChannel"
            request-channel="apiChannel"
            reply-channel="jsonToObjectChannel"
                        ....
            />
         <int:json-to-object-transformer type="test.Response" input-channel="jsonToObjectChannel" output-channel="responseChannel"/>

Above code works till spring-integration version 5.1. When I upgrade to 5.2. It starts to throw the exception as org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [test.Request] to type [test.Response].

I have noticed that object-to-json-transformer add class type on the header with key json__TypeId__. Then it uses that class type for json-to-object-transformer.

But it is expected that type attribute mentioned on json-to-object-transformer should be used if mentioned.

Please suggest on fixing this issue or Is it really bug on spring integration (5.2).


Solution

  • Consider to add a <header-filter header-names="json_*" /> before calling your REST service. The <int:object-to-json-transformer> populates JsonHeaders to let downstream to know what the real type of JSON we curry in the payload.

    A <int:json-to-object-transformer> prefers those headers instead of static type option. But since the payload is already a different representation than those request headers it does a wrong thing.

    I would suggest an option on the <int:json-to-object-transformer> to make a preference, but that would not be fully logical. Since we have changed a payload, it would be better to change its respective headers. Otherwise we just lying to ourselves.

    On the other hand a HTTP Outbound Gateway can take care for your to convert request into a JSON for network and back from JSON response to some POJO type.

    See https://docs.spring.io/spring-integration/docs/5.2.3.RELEASE/reference/html/http.html#http-outbound and its expected-response-type. As long as a contentType header is an application/json, you are good to avoid those <int:object-to-json-transformer> & <int:json-to-object-transformer>.