rest - springboot rest swagger:初始化bean时出错
问题描述
我正在使用 swagger 来记录我的 spring boot rest 应用程序
- spring-boot-starter-web
- spring-boot-starter-data-rest
- springfox-swagger2 2.9.2
- springfox-swagger 2.9.2
代码构建良好,但是当我运行它时,它在下面抛出错误
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;
解决方案
我有同样的问题。这是我的修复:
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
解决方案
- Springfox 显然已被放弃: Springfox swagger not working in spring boot 2.2.0
- 解决方案是迁移到不同的 spring-swagger 桥。
最有可能的候选人是 Springdoc-openapi: https ://springdoc.github.io/springdoc-openapi-demos/
用户体验(来自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>
推荐阅读
- modelica - 如何在 Modelica 中构建液体平衡连接器?
- reactjs - 在 init 之前调用 React Redux 操作
- git - 如何丢弃 Git 中的 stash pop 而不丢弃我未提交的更改?
- cmake - MSys2 Mingw --major-image-version
- mysql - 无法处理 MYSQL 函数中的异常
- haskell - Haskell中关于随机数的另一个问题
- c# - 仅显示 WPF DataGrid 中数据库中的重复项
- java - CONNECTIVITY_ACTION 广播接收器不工作
- dbeaver - SELECT AS 隐式转换列
- android - 如何在 Android 中使用 PlaybackParams 保存录音?