spring-bootjacksonobjectmapperjackson-databindjackson-modules

Jackson object mappers mixin breaks springboot list response from controller


I have the following situation where i have multiple MappingJackson2HttpMessageConverter both of them using a shared ObjectMapper that has a single mixin added on it.

@Bean
public ObjectMapper objectMapper(){
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.addMixin(Item.class, ItemMixin.class);
    return objectMapper;
}
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(objectMapper());
    return mappingJackson2HttpMessageConverter;
}

The application is SpringBoot(i know that springboot does its own default instantiation of message converters) but the problem is that when i call an endpoint that has a list of items returned i get a 500 because the line:

objectMapper.addMixin(Item.class, ItemMixin.class);

somehow breaks and i don't understand why.

@GetMappring(.....)
public List<Item> getItems(){
    return Arrays.asList(new Item(1), new Item(2));
}

I know that by default spring boot controller returns a string json if nothing is specified.The code is simplified for better readability.


Solution

  • It turns out that if one of the custom serializers that are used by the mixin by any chance throw an exception in the overriden serialize method, then that exception causes that nasty HTTP 500 when calling the exposed Rest endpoint. Curious is that the message is not that visible and straightforward interpretable.