spring-cloud / spring-cloud-stream

Framework for building Event-Driven Microservices
http://cloud.spring.io/spring-cloud-stream
Apache License 2.0
1.01k stars 612 forks source link

Configured object mapper will be ignored by Spring Cloud Stream 4.1.3 when serialising messages #2977

Closed wrwksexahatvani closed 1 month ago

wrwksexahatvani commented 3 months ago

Describe the issue Configured object mapper will be ignored when serializing messages leading to a deserialisation error in an integration test.

To Reproduce Build and test https://github.com/andrashatvani/spring-demo/ before and after setting Spring Cloud 2023.0.3 in the POM.

Version of the framework Spring Cloud 2023.0.3

Expected behavior All tests pass.

superdanio commented 2 months ago

Just in case this helps anybody else, I've recently come across the same issue, and after some debugging I realised that the SmartCompositeMessageConverter used behind the scenes was getting initialised by default with a collection of MessageConverters, one of which was a org.springframework.cloud.function.context.config.JsonMessageConverter that used internally its own ObjectMapper (not getting it from the application context).

I've managed to work around it by defining my own Bean like this:

    @Bean
    public JsonMessageConverter customJsonMessageConverter(ObjectMapper objectMapper) {
        return new JsonMessageConverter(new JacksonMapper(objectMapper));
    }

That bean came first within the collection of converters automatically injected in SmartCompositeMessageConverter and solved the issue for me.

olegz commented 2 months ago

This has just been resolved in spring-cloud-function - https://github.com/spring-cloud/spring-cloud-function/pull/1162 Having said that, the solution from @superdanio is not really a workaround, rather a standard way to extend/change/override something in Spring..

Give i a shot and let me know so we can close it.