首页 > 解决方案 > springboot rest swagger:初始化bean时出错

问题描述

我正在使用 swagger 来记录我的 spring boot rest 应用程序

代码构建良好,但是当我运行它时,它在下面抛出错误

org.springframework.beans.factory.UnsatisfiedDependencyException: 
    Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration 

$EnableWebMvcConfiguration.class]:
 Unsatisfied dependency expressed through method 'requestMappingHandlerMapping' parameter 1;
nested exception is org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration

$EnableWebMvcConfiguration.class]:
 Bean instantiation via factory method failed;
 nested exception is org.springframework.beans.BeanInstantiationException:
 Failed to instantiate [org.springframework.format.support.FormattingConversionService]: 
Factory method 'mvcConversionService' threw exception; 
nested exception is org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'jsonSchemaConverter' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]:
     Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
 Failed to instantiate [org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter]:
     Factory method 'jsonSchemaConverter' threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException: 
     Error creating bean with name 'defaultConversionService' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]:
     Bean instantiation via factory method failed;  
nested exception is org.springframework.beans.BeanInstantiationException: 
     Failed to instantiate [org.springframework.format.support.DefaultFormattingConversionService]:
     Factory method 'defaultConversionService' threw exception; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
     Error creating bean with name 'repositoryInvokerFactory' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]:
     Bean instantiation via factory method failed;

nested exception is org.springframework.beans.BeanInstantiationException:
 nested exception is org.springframework.beans.BeanInstantiationException:
     Failed to instantiate [org.springframework.data.repository.support.RepositoryInvokerFactory]:
     Factory method 'repositoryInvokerFactory' threw exception;

nested exception is java.lang.NoSuchMethodError:      org.springframework.plugin.core.PluginRegistry.of(Ljava/util/List;)Lorg/springframework/plugin/core/PluginRegistry;   

标签: restspring-bootswagger

解决方案


我有同样的问题。这是我的修复:

TL/DR:

Springfox 使用的 spring-plugin-core 版本过时,与 spring boot 2.2.5 不兼容。Springfox 似乎不再维护;迁移到不同的spring-swagger-bridge,比如Springdoc-openapi:https ://springdoc.github.io/springdoc-openapi-demos/


相关输出

nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.data.repository.support.RepositoryInvokerFactory]:
Factory method 'repositoryInvokerFactory' threw exception; nested exception is java.lang.NoSuchMethodError:
'org.springframework.plugin.core.PluginRegistry org.springframework.plugin.core.PluginRegistry.of(java.util.List)'

来自NoSuchMethodError: org.springframework.plugin.core.PluginRegistry.getPluginOrDefaultFor

...一个疯狂的猜测是 springfox 的最后一个工作版本(为您和我们工作)是使用低于 2.2.2 的 Spring boot 版本编译的。该引导版本具有不同的 getPluginOrDefaultFor 方法签名(或者该方法可能根本不存在)。


原因

  • springfox 导入 spring-plugin-core 的 1.2.0 版本。Spring Boot 2.2.5 需要版本 2.0.0.RELEASE
  • springfox 中了进口彩票;测试失败 b/c Spring Boot 找不到它的方法。
  • 显式导入 2.0.0.RELEASE 会使 springfox 死掉。
  • springfox 的最新版本是 2.9.2;即使 3.0.0-SNAPSHOT 也不能解决问题:https ://github.com/springfox/springfox/issues/2932


解决方案

是的,我最终点击了“springDoc”的链接并找到了一个快速教程。在 pom.xml 文件中设置所有内容非常容易。只需要一次调整
<!-- org.springdoc for swagger setup  -->
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-core</artifactId>
    <version>1.1.49</version>
    <exclusions>
        <exclusion>
            <groupId>io.github.classgraph</groupId>
            <artifactId>classgraph</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.1.49</version>
</dependency>

推荐阅读