无法使用 Spring Fox 启动 bean 'documentationPluginsBootstrapper' Spring boot Swagger 实现?

Raj*_*mar 10 swagger spring-boot springfox

我在 Spring Boot 应用程序中使用 springfox-boot-starter 依赖项来获取 swagger UI,但是当我尝试运行该应用程序时,出现以下错误

    Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NoSuchMethodError: org.springframework.plugin.core.PluginRegistry.getPluginFor(Ljava/lang/Object;)Ljava/util/Optional;
        at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:184)
        at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:52)
        at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356)
        at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:157)
        at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:121)
        at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:885)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:161)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
Run Code Online (Sandbox Code Playgroud)

我的配置文件:


 @Bean
 public Docket productApi() {
        
     return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.swagger.demo.controller"))
        .paths(regex("/student.*"))
        .build()
        .enableUrlTemplating(true)
        .produces(DEFAULT_PRODUCES_AND_CONSUMES)
        .consumes(DEFAULT_PRODUCES_AND_CONSUMES)
        .apiInfo(apiEndPointsInfo());
    }

private ApiInfo apiEndPointsInfo() {

    return new ApiInfoBuilder()
            .title("demo")
            .description("demo")
            .version("1.0")
            .build();
} 

Run Code Online (Sandbox Code Playgroud)

以下是我正在使用的依赖项。 pom.xml 文件:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
    
<dependencies>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-boot-starter</artifactId>
      <version>3.0.0</version>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

为什么我会出现上述错误,我什么也没得到。任何帮助将不胜感激。提前致谢。

小智 26

我有类似的问题并在这里找到了解决方案。基本上,您必须在文件中包含application.properties以下配置:

spring.mvc.pathmatch.matching-strategy=ant-path-matcher
Run Code Online (Sandbox Code Playgroud)

看来 Spring Boot 2+ 将基于 PathPathern 的匹配器设置为默认值,而 Spring Fox 则期望基于 Ant 的匹配器。此解决方案的问题是您无法使用 Spring Actuator,因为它使用基于 PathPattern 的 URL 匹配。在这种情况下,请检查Spring Fox 问题