Search code examples
springspring-cloud-streamspring-rabbitjackson-databindspring-messaging

Spring Cloud Stream @StreamListener Custom MappingJackson2MesageConverter


I have a custom ObjectMapper configured within my application which has custom modules for deserializing immutable types provided by Guava in Jackson.

My problem is that I cannot seem to override the objectMapper use by @StreamListener such that it deserializes my objects correctly.

Here is what I have tried:

@Bean
public MessageConverter messageConverter() {
    final MappingJackson2MessageConverter mappingJackson2MessageConverter = new MappingJackson2MessageConverter();
    mappingJackson2MessageConverter.setObjectMapper(objectMapper);
    return mappingJackson2MessageConverter;
}

The above snippet is in a class annotated with @Configuration and the objectMapper is defined in the class:

    public static final ObjectMapper objectMapper = new ObjectMapper()
        .registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES))
        .registerModule(new GuavaModule());

Any ideas on what I may be missing?

  • Spring Boot: 1.5.10.RELEASE
  • Spring Rabbit: 1.7.6.RELEASE
  • Spring Cloud Stream: 1.2.2.RELEASE
  • Spring Messaging: 4.3.14.RELEASE

Solution

  • So, it turns out there was no issue with my configuration. My custom ObjectMapper was being selected by the MappingJackson2MessageConverter. However, the message converter was not being selected, because the messages were being sent with the wrong content-type header.

    Moral of the story, be careful with the Content-Type and make sure it matches the converter.