0

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.

Shvalb
  • 1,835
  • 2
  • 30
  • 60
  • 1
    You did try `@Configuration` class extends `WebMvcAutoConfiguration` class? – fatih Jul 07 '22 at 22:36
  • Did you also debug the serialization of your `Object` instance, to see how and which serializer Jackson uses ? – hc_dev Jul 07 '22 at 22:45
  • @faith yep, and it didn't help...it actually gave me other type of exception in which I had to add some spring features in my application.properties file. – Shvalb Jul 07 '22 at 23:13
  • @hc_dev - I have a breakpoint in my custom serializer but it never reaches this code. I could try to debug from the place where I return the object from the Controller ...hopefully understand which serializer it uses ...but this is pretty complex and like a rabbit hole..so much springboot code. – Shvalb Jul 07 '22 at 23:15
  • Is there any meaning to the order you register the modules in `ObjectMapper` ? – Shvalb Jul 07 '22 at 23:30
  • @faith - I take it back!! `WebMvcAutoConfiguration` is actually working!! I only needed to put `spring.mvc.pathmatch.matching-strategy = ant_path_matcher` in my `application.properties`. You ROCK!! – Shvalb Jul 08 '22 at 00:06

1 Answers1

0

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.

Ausgefuchster
  • 1,091
  • 5
  • 14
  • It doesn't use the `Serializer` in the custom `Module`. – Shvalb Jul 07 '22 at 20:59
  • Yeah and I think the problem is one layer above. If youre serialization happens in a Controller class my guess is that Spring doesnt even use the customized ObjectMapper, because this was the case for me. Therefore I had to use this annotation because it works without changing the ObjectMapper. – Ausgefuchster Jul 08 '22 at 09:02