@Configuration
@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class JacksonConfig {
private final ObjectMapper objectMapper;
public JacksonConfig(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Bean
public Module module() {
// this module didn't work
return new CusModule();
}
@PostConstruct
public void postConstruct() {
// this filter works
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.addFilter(JsonFilterName.HAS, new AnnotationFilter());
objectMapper.setFilterProvider(filterProvider);
}
}
@Configuration
@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class JacksonConfig {
private final ObjectMapper objectMapper;
public JacksonConfig(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@PostConstruct
public void postConstruct() {
// filter and module both works
objectMapper.registerModule(new CusModule());
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.addFilter(JsonFilterName.HAS, new AnnotationFilter());
objectMapper.setFilterProvider(filterProvider);
}
}
Comment From: philwebb
I suspect that trying to inject that ObjectMapper into the @Configuration class that provides the module is causing the problem. The Module beans are applied when the ObjectMapper is created. In your example, the JacksonConfig class needs to be created in order to get the Module instance, but by creating it you are forcing early initialization so that it can be injected into the constructor.
You can try changing the module() method to static, but I'd probably recommend that you create the ObjectMapper in a @Bean method rather than using postConstruct(). Something like:
@Configuration
public class JacksonConfig {
@Bean
ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper mapper = builder.createXmlMapper(false).build();
objectMapper.registerModule(new CusModule());
SimpleFilterProvider filterProvider = new SimpleFilterProvider();
filterProvider.addFilter(JsonFilterName.HAS, new AnnotationFilter());
objectMapper.setFilterProvider(filterProvider);
return objectMapper;
}
}
As a side-note, you don't need to use @AutoConfigureAfter unless you are developing your own auto-configuration class.
Comment From: lin-mt
I suspect that trying to inject that
ObjectMapperinto the@Configurationclass that provides the module is causing the problem. TheModulebeans are applied when theObjectMapperis created. In your example, theJacksonConfigclass needs to be created in order to get theModuleinstance, but by creating it you are forcing early initialization so that it can be injected into the constructor.You can try changing the
module()method to static, but I'd probably recommend that you create theObjectMapperin a@Beanmethod rather than usingpostConstruct(). Something like:```java @Configuration public class JacksonConfig {
@Bean ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {ObjectMapper mapper = builder.createXmlMapper(false).build(); objectMapper.registerModule(new CusModule()); SimpleFilterProvider filterProvider = new SimpleFilterProvider(); filterProvider.addFilter(JsonFilterName.HAS, new AnnotationFilter()); objectMapper.setFilterProvider(filterProvider); return objectMapper; }
} ```
As a side-note, you don't need to use
@AutoConfigureAfterunless you are developing your own auto-configuration class.
Yes! This is better, thank you!
Comment From: lin-mt
I suspect that trying to inject that
ObjectMapperinto the@Configurationclass that provides the module is causing the problem. TheModulebeans are applied when theObjectMapperis created. In your example, theJacksonConfigclass needs to be created in order to get theModuleinstance, but by creating it you are forcing early initialization so that it can be injected into the constructor.You can try changing the
module()method to static, but I'd probably recommend that you create theObjectMapperin a@Beanmethod rather than usingpostConstruct(). Something like:```java @Configuration public class JacksonConfig {
@Bean ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {ObjectMapper mapper = builder.createXmlMapper(false).build(); objectMapper.registerModule(new CusModule()); SimpleFilterProvider filterProvider = new SimpleFilterProvider(); filterProvider.addFilter(JsonFilterName.HAS, new AnnotationFilter()); objectMapper.setFilterProvider(filterProvider); return objectMapper; }
} ```
As a side-note, you don't need to use
@AutoConfigureAfterunless you are developing your own auto-configuration class.
anather way,this Module is works too.
@Configuration
@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class JacksonConfig {
private final ObjectMapper objectMapper;
public JacksonConfig(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Bean
public Module module() {
// it works
return new CusModule();
}
}