8

In application context I have registered an ObjectMapper module:

@Bean
public SimpleModule jsr310Module() {
    final SimpleModule module = new SimpleModule();
    module.addSerializer(new LocalDateSerializer());
    module.addDeserializer(LocalDate.class, new LocalDateDeserializer());
    return module;
}

I tried to debug and it is loaded (or at least the method public void setupModule(SetupContext context) is execute on boot) but when I call a rest API that return an object with a LocalDate my deserializer is ignored.

Some hint to solve the problem?

rascio
  • 8,968
  • 19
  • 68
  • 108
  • How have you registered the ObjectMapper? As the SimpleModule needs to be injected into the ObjectMapper. – Augusto Apr 11 '15 at 21:00
  • I didn't configured it, the module should be configureted by default, and it does it...I can also use another way, what do you suggest? – rascio Apr 11 '15 at 21:09
  • You can look at the class `JacksonAutoConfiguration` from spring-boot and try to debug it a bit. I'm reading it and trying to figure out how the hell it manages to configure the ObjectMapper. It looks like if the `LocalDateDeserializer` class (from version 1.2.2) is in the classpath, it should load it automatically as part of `JodaDateTimeJacksonConfiguration`. (I still have trouble understanding this type of configuration :S) – Augusto Apr 11 '15 at 21:19
  • @rascio "when I call a rest API that return an object with a LocalDate my deserializer is ignored." you mean the _serializer_ is ignored right? – ci_ Apr 12 '15 at 10:14
  • yes, sorry...In the end I solved, it was that I miss to extends the `WebMvcAutoConfiguration` class... – rascio Apr 12 '15 at 10:32

2 Answers2

2

To make it work the @Configuration class should extend WebMvcAutoConfiguration

rascio
  • 8,968
  • 19
  • 68
  • 108
  • how should this one know :(? there is no mention in docs just `Any beans of type com.fasterxml.jackson.databind.Module are automatically registered with the auto-configured Jackson2ObjectMapperBuilder and are applied to any ObjectMapper instances that it creates. This provides a global mechanism for contributing custom modules when you add new features to your application.` – kqr Jul 17 '19 at 12:57
1

According to the Spring Boot documentation, to configure the ObjectMapper you can either define the bean yourself and annotate it with @Beanand @Primary. There you can register the module. Or you can add a bean of type Jackson2ObjectMapperBuilderwhere you can customize the ObjectMapper.

dunni
  • 43,386
  • 10
  • 104
  • 99
  • 5
    The same documentation you've linked also says "Another way to customize Jackson is to add beans of type com.fasterxml.jackson.databind.Module to your context. They will be registered with every bean of type ObjectMapper, providing a global mechanism for contributing custom modules when you add new features to your application." It looks like this is what the original poster is trying to do. – ci_ Apr 12 '15 at 10:08