问题描述
在我的春季启动应用程序中,我使用Jackson通过在需要时注入 ObjectMapper
来序列化对象。
我找到了这个答案:
但是这个创建了一个新的映射器 - 使用jacksons默认设置。
In my spring boot application i am using Jackson to serialize objects by injecting the ObjectMapper
where needed.I found this answer: https://stackoverflow.com/a/32842962/447426But this one creates a new mapper - with jacksons default settings.
另一方面,我发现。我真的不明白。没有示例代码。
On the other hand i found this in official docu. I didn't really understood. There is no example code.
那么如何在Springs默认对象映射器的基础上配置spring ObjectMapper?
So how to configure springs ObjectMapper on base of Springs default object mapper?
这个配置应该在注入的ObjectMapper上激活。
This configuration should be active on "ObjectMapper" whereever injected.
推荐答案
你应该使用 Jackson2ObjectMapperBuilderCustomizer
为此
@Configuration
public class JacksonConfiguration {
@Bean
public Jackson2ObjectMapperBuilderCustomizer addCustomBigDecimalDeserialization() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
jacksonObjectMapperBuilder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// Add your customization
// jacksonObjectMapperBuilder.featuresToEnable(...)
}
};
}
}
这篇关于如何在弹簧启动应用程序中配置Jackson而不覆盖纯java中的弹簧默认设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!