Adding custom Jackson Module to Spring boot's ObjectMapper is ignored

796 Views Asked by At

I use SpringBoot v2.2.2RELEASE (very old version) and I configured a custom Module as follow:

@Configuration
public class JacksonConfig {

@Bean
public Module jacksonModule() {

  SimpleModule module = new SimpleModule();
  module.addSerializer(Object.class, new MyCustomSerializer());
  return module;
 }
}

This works well and it does what it supposed to do.

Recently, I've upgraded to the lates SpringBoot 2.7.0 and the serializer gets ignored! I can see that when my app loads then it instantiates a new Module instance, but it doesn't call the serializer anymore...

(What the serializer does is add a root node to the response (json) that returns to the client via REST Controller).

Any idea?

EDIT1

I tried to debug it by put a breakpoint in JacksonAuthConfiguration#configureModules and I saw the module in the list as well as some other Swagger related modules.

1

There are 1 best solutions below

2
Ausgefuchster On

So if I understand it correct your problem is that Spring doesnt take this ObjectMapper for the RestController. And in this case I struggled with this problem aswell. I tried everything from defining a primary bean for ObjectMapper, defining a bean for the factor adding the module as you did but nothing worked.

So in the end my solution was to use @JsonDeserialize and @JsonSerialize annotations.