[英]How to enable “Global default typing” in Jackson via “jackson2ObjectMapperBuilder”
[英]How to force Jackson2ObjectMapperBuilder in spring-boot?
我要创建自己的jackson
ObjectMapper
bean,如下所示:
@SpringBootApplication
@AutoConfigureBefore(JacksonAutoConfiguration.class) //even this does not help
public class MyConfig extends SpringBootServletInitializer {
@Bean
@Primary
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(true).build(); //much more configurations
}
}
问题:永远不会创建该bean,而是执行默认的JacksonAutoConfiguration
:
package org.springframework.boot.autoconfigure.jackson;
@Configuration
@ConditionalOnClass(ObjectMapper.class)
public class JacksonAutoConfiguration {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return builder.createXmlMapper(false).build(); //this is always executed!
}
}
因此,当评估JacksonAutoConfiguration
时, ObjectMapper
bean还不存在。 但为什么?
通过使用断点进行调试,我还可以看到从未调用过我的bean! 但是我注意到的是:即使我有@AutoConfigureBefore
,杰克逊自动配置仍在MyConfig
任何bean 之前运行。 奇怪?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.