java - 更新到 Spring Boot 2 后 Jackson 模块未注册
问题描述
我正在从 Spring Boot 1.5.21 升级到 2.2.5。
我需要用来MonetaryModule
反序列化休息调用,并取决于 Spring 的ObjectMapper
.
我在某个@Configuration
类(MonetaryModule
正在扩展Module
)中为这个模块定义了这样一个 bean:
@Bean
public MonetaryModule monetaryModule() {
return new MonetaryModule();
}
我可以在/beans
端点中看到它已创建。但是,它实际上并未加载到ObjectMapper
. 在对 Spring 的代码进行大量调试和挖掘之后,我得出的结论是JacksonAutoConfiguration
. 它有一个名为的内部静态类JacksonObjectMapperBuilderConfiguration
,其中有一个bean
创建Jackson2ObjectMapperBuilder
. 在创建过程中customize()
,最终会调用到此代码:
private void configureModules(Jackson2ObjectMapperBuilder builder) {
Collection<Module> moduleBeans = getBeans(this.applicationContext, Module.class);
builder.modulesToInstall(moduleBeans.toArray(new Module[0]));
}
这段代码似乎负责将模块加载到ObjectMapper
中,问题是这Jackson2ObjectMapperBuilder
实际上并没有创建。它出现在/beans
端点中,但事实上当我在那里断点时我没有达到断点。这解释了为什么模块没有加载到ObjectMapper
.
问题是,为什么不调用此代码?为什么/bean
表明 bean 确实存在?
编辑 - 添加条件评估报告:
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
-----------------
AopAutoConfiguration matched:
- @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)
AopAutoConfiguration.AspectJAutoProxyingConfiguration matched:
- @ConditionalOnClass found required class 'org.aspectj.weaver.Advice' (OnClassCondition)
AopAutoConfiguration.AspectJAutoProxyingConfiguration.CglibAutoProxyConfiguration matched:
- @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)
ConfigServiceBootstrapConfiguration#configServicePropertySource matched:
- @ConditionalOnProperty (spring.cloud.config.enabled) matched (OnPropertyCondition)
- @ConditionalOnMissingBean (types: org.springframework.cloud.config.client.ConfigServicePropertySourceLocator; SearchStrategy: all) did not find any beans (OnBeanCondition)
ConfigServiceBootstrapConfiguration.RetryConfiguration matched:
- @ConditionalOnClass found required classes 'org.springframework.retry.annotation.Retryable', 'org.aspectj.lang.annotation.Aspect', 'org.springframework.boot.autoconfigure.aop.AopAutoConfiguration' (OnClassCondition)
- @ConditionalOnProperty (spring.cloud.config.fail-fast) matched (OnPropertyCondition)
ConfigServiceBootstrapConfiguration.RetryConfiguration#configServerRetryInterceptor matched:
- @ConditionalOnMissingBean (names: configServerRetryInterceptor; SearchStrategy: all) did not find any beans (OnBeanCondition)
ConfigurationPropertiesRebinderAutoConfiguration matched:
- @ConditionalOnBean (types: org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; SearchStrategy: all) found bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor' (OnBeanCondition)
ConfigurationPropertiesRebinderAutoConfiguration#configurationPropertiesBeans matched:
- @ConditionalOnMissingBean (types: org.springframework.cloud.context.properties.ConfigurationPropertiesBeans; SearchStrategy: current) did not find any beans (OnBeanCondition)
ConfigurationPropertiesRebinderAutoConfiguration#configurationPropertiesRebinder matched:
- @ConditionalOnMissingBean (types: org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder; SearchStrategy: current) did not find any beans (OnBeanCondition)
EncryptionBootstrapConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.security.crypto.encrypt.TextEncryptor' (OnClassCondition)
PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer matched:
- @ConditionalOnMissingBean (types: org.springframework.context.support.PropertySourcesPlaceholderConfigurer; SearchStrategy: current) did not find any beans (OnBeanCondition)
Negative matches:
-----------------
AopAutoConfiguration.AspectJAutoProxyingConfiguration.JdkDynamicAutoProxyConfiguration:
Did not match:
- @ConditionalOnProperty (spring.aop.proxy-target-class=false) did not find property 'proxy-target-class' (OnPropertyCondition)
AopAutoConfiguration.ClassProxyingConfiguration:
Did not match:
- @ConditionalOnMissingClass found unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
DiscoveryClientConfigServiceBootstrapConfiguration:
Did not match:
- @ConditionalOnProperty (spring.cloud.config.discovery.enabled) did not find property 'spring.cloud.config.discovery.enabled' (OnPropertyCondition)
EncryptionBootstrapConfiguration.RsaEncryptionConfiguration:
Did not match:
- Keystore nor key found in Environment (EncryptionBootstrapConfiguration.KeyCondition)
Matched:
- @ConditionalOnClass found required class 'org.springframework.security.rsa.crypto.RsaSecretEncryptor' (OnClassCondition)
EncryptionBootstrapConfiguration.VanillaEncryptionConfiguration:
Did not match:
- @ConditionalOnMissingClass found unwanted class 'org.springframework.security.rsa.crypto.RsaSecretEncryptor' (OnClassCondition)
Exclusions:
-----------
None
Unconditional classes:
----------------------
None
此外,附加的是/conditions
&/beans
端点输出的输出(太大而无法粘贴到帖子本身)
解决方案
您的状况评估报告显示以下内容:
"JacksonAutoConfiguration.JacksonObjectMapperConfiguration#jacksonObjectMapper": {
"notMatched": [
{
"condition": "OnBeanCondition",
"message": "@ConditionalOnMissingBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) found beans of type 'com.fasterxml.jackson.databind.ObjectMapper' jacksonBuilder"
}
],
"matched": []
},
这意味着 Spring Boot 自动配置条件正在退出,因为您的应用程序已经就此事提供了意见:已经有一个名为;的ObjectMapper
bean 。jacksonBuilder
它由您正在使用的库中的应用程序或某些配置类提供。
在这种情况下,这来自配置类,正如 bean 端点所指出的:
"jacksonBuilder": {
"aliases": [],
"scope": "singleton",
"type": "com.fasterxml.jackson.databind.ObjectMapper",
"resource": "class path resource [com/behalf/core/authorization_domain/config/AuthorizationDomainConfiguration.class]",
"dependencies": []
},
您应该在这里与此维护者取得联系,AuthorizationDomainConfiguration
并让他们知道他们应该避免覆盖应用程序对 JSON(反)序列化的意见。
推荐阅读
- java - 带有 sslSocketFactory 的 Okhttp.Builder 中的 ExceptionInInitializerError
- angular - 如何仅从 Angular 6 中的 http 请求中获取特定字段
- jquery - Jquery Append 不工作;“请删除换行符并编码小于字符...”在 F12 处引发错误
- python - 使用 Keras 对 3 个类进行图像分类仅返回一个值而不是 1 X 3 数组
- python - OpenCV 绘制轮廓错误断言失败
- android - 在 kotlin 中创建对象的语法有些模糊?
- codeigniter - 重定向时没有删除 index.php - codeigniter
- javascript - Vue prop - 在字符串中同时传递单引号和双引号
- php - 为不同的请求生成相同的序列号
- python - 将窗口定义为类,但无法调用 mainloop 函数