Spring Boot 升级后“找不到提供程序 com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule”

Posted

技术标签:

【中文标题】Spring Boot 升级后“找不到提供程序 com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule”【英文标题】:"Provider com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule not found" after Spring Boot Upgrade 【发布时间】:2020-05-12 22:55:21 【问题描述】:

在将 Spring Boot 从 2.1.5 升级到 2.2.2 后,当我尝试启动应用程序时收到此异常消息。

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'feignContract' defined in org.springframework.cloud.openfeign.FeignClientsConfiguration: Unsatisfied dependency expressed through method 'feignContract' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feignConversionService' defined in org.springframework.cloud.openfeign.FeignClientsConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'feignConversionService' threw exception; nested exception is java.util.ServiceConfigurationError: com.fasterxml.jackson.databind.Module: Provider com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule not found
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    at org.springframework.cloud.context.named.NamedContextFactory.createContext(NamedContextFactory.java:136)
    at org.springframework.cloud.context.named.NamedContextFactory.getContext(NamedContextFactory.java:101)
    at org.springframework.cloud.context.named.NamedContextFactory.getInstance(NamedContextFactory.java:145)
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.get(FeignClientFactoryBean.java:225)
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.feign(FeignClientFactoryBean.java:86)
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:262)
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:252)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171)
    ... 169 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'feignConversionService' defined in org.springframework.cloud.openfeign.FeignClientsConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'feignConversionService' threw exception; nested exception is java.util.ServiceConfigurationError: com.fasterxml.jackson.databind.Module: Provider com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule not found
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:484)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)
    ... 189 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'feignConversionService' threw exception; nested exception is java.util.ServiceConfigurationError: com.fasterxml.jackson.databind.Module: Provider com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule not found
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
    ... 203 common frames omitted
Caused by: java.util.ServiceConfigurationError: com.fasterxml.jackson.databind.Module: Provider com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule not found
    at java.util.ServiceLoader.fail(ServiceLoader.java:239)
    at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
    at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:372)
    at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
    at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
    at com.fasterxml.jackson.databind.ObjectMapper.findModules(ObjectMapper.java:1054)
    at com.fasterxml.jackson.databind.ObjectMapper.findModules(ObjectMapper.java:1038)
    at com.fasterxml.jackson.databind.ObjectMapper.findAndRegisterModules(ObjectMapper.java:1088)
    at at.porscheinformatik.retail.integration.api.formatter.DefaultDateTimeFormatter.<init>(DefaultDateTimeFormatter.java:24)
    at at.porscheinformatik.sbo.integration.impl.rest.feign.RestIntegrationFeignConfiguration.registerFormatters(RestIntegrationFeignConfiguration.java:30)
    at org.springframework.cloud.openfeign.FeignClientsConfiguration.feignConversionService(FeignClientsConfiguration.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)

当我刚刚升级到 Spring Boot 2.2.1 时,不会发生此异常。

添加依赖后:

<dependency>
      <groupId>com.fasterxml.jackson.module</groupId>
      <artifactId>jackson-module-jaxb-annotations</artifactId>
</dependency>

它适用于 2.2.2。

所以我的问题是,为什么我在这次 Spring Boot 升级后突然需要手动添加依赖项。

【问题讨论】:

使用两个版本的 Spring Boot 运行 mvn dependency:tree 并比较 jackson-module-jaxb-annotations 的结果。我的猜测是在版本 2.2.2 中更新了 Sprint Boot 依赖项之一(内部可能已删除 jackson-module-jaxb-annotations),需要您显式添加它。 我已经尝试过了,但是两个版本的树中都没有 jackson-module-jaxb-annotations 依赖项。 @AlexanderDo 你用的是什么java版本? @SupunWijerathne 它采用 OpenJDK 版本 8.0.232.09 【参考方案1】:

我会查看可能使用 xml 作为配置的依赖项。我使用的是 Spring Boot 2.3.1,并且在添加 Liquibase 支持之前不需要这种依赖关系。由于我使用 xml 进行 Liquibase 配置,因此我在启动时看到相同的错误。一旦我添加了这个依赖,错误就消失了。

【讨论】:

我确认在 Spring Boot 2.3.3.RELEASE 中支持 Liquibase 期间发生了同样的问题,并且在添加 jackson-module-jaxb-annotations 后问题消失了。【参考方案2】:

假设您的应用程序正在使用/包括 Liquibase,这是因为 liquibase-core 中有一个错误文件,该文件定义了 Jackson 模块的 SPI 扩展,但未提供该模块的实现:https://github.com/liquibase/liquibase/issues/1051

OP 中列出的解决方法就足够了,因为它为 SPI 文件中引用的 Jackson 模块提供了实现,但您也可以将 Liquibase 升级到 >= 3.10.1 来解决这个问题(我只测试了 3.10.3 但根据链接的缺陷 3.10.1 应该可以工作)

对于 Gradle,依赖项是:

implementation 'org.liquibase:liquibase-core:3.10.3'

更多信息:https://github.com/logstash/logstash-logback-encoder/issues/387#issuecomment-581603444

【讨论】:

事实上我正在使用 Liquibase,所以我会看看这个问题。如果解决方法有效,我将发布答案。谢谢! 如果您想留在3.8.x 发布序列中,您还应该能够将 liquibase 降级到 3.8.0

以上是关于Spring Boot 升级后“找不到提供程序 com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule”的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot:REST API 行为不一致后版本升级

将 Web 应用升级到 Spring Boot 2.4 后出现 IllegalStateException

升级到 Spring Boot 版本 2 后 Prometheus 无法正常工作

Spring Boot - 从 2.2.5 升级到 2.3.0 后验证停止工作

升级到 Spring Boot 2.6.1 和 Spring Cloud 2021.0.0 后 Spring Cloud Stream 启动错误

升级到 Spring Boot 2.2.2 后应用程序启动缓慢