gpt4 book ai didi

java - 更新到 Spring Boot 2 后 Jackson 模块未注册

转载 作者:行者123 更新时间:2023-12-01 11:17:49 26 4
gpt4 key购买 nike

我正在从 Spring Boot 1.5.21 升级到 2.2.5。

我需要使用 MonetaryModule反序列化休息调用,并取决于 Spring 的 ObjectMapper .

我在一些 @Configuration 中为这个模块定义了这样一个 bean。类( MonetaryModule 正在扩展 Module ):

@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端点输出(太大而无法粘贴到帖子本身)

endpoints output

最佳答案

您的状况评估报告显示以下内容:

        "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 - 更新到 Spring Boot 2 后 Jackson 模块未注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61656985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com