Spring Boot ignore ObjectMapper module

余生颓废 提交于 2019-12-07 01:06:01

问题


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?


回答1:


To make it work the @Configuration class should extend WebMvcAutoConfiguration




回答2:


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.



来源:https://stackoverflow.com/questions/29582885/spring-boot-ignore-objectmapper-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!