Swagger 2 问题 spring Boot

Shu*_*Rai 7 java swagger-ui spring-boot swagger-2.0

我在 Spring Boot 中面临 Swagger 集成问题。查看代码和错误片段。

------------------POM--------------------

<properties>
    <java.version>1.8</java.version>
    <swagger.version>2.9.2</swagger.version>
</properties>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-bean-validators</artifactId>
    <version>${swagger.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

-----------------应用类--------------

@SpringBootApplication
@EnableSwagger2
public class ProducerApplication {

  public static void main(String[] args) {
    SpringApplication.run(ServletPocProducerApplication.class, args);
  }
  
  @Bean 
  public Docket api() { 
    return new Docket(DocumentationType.SWAGGER_2)
      .select() 
      .apis(RequestHandlerSelectors.any())
      .paths(PathSelectors.any())
      .build();
  }
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪

org.springframework.context.ApplicationContextException: Failed to start bean 
'documentationPluginsBootstrapper'; nested exception is 
 java.lang.NullPointerException: Cannot invoke 
"org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" 
because the return value of 
"springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.docume 
ntation.RequestHandler)" is null
   at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.13.jar:5.3.13]
Run Code Online (Sandbox Code Playgroud)

我该如何解决??

小智 21

我通过在 application.properties 中添加“spring.mvc.pathmatch.matching-strategy=ant-path-matcher”解决了这个问题。


小智 10

很长一段时间我一直试图解决这个问题,解决方案是:

a) 将其添加到 application.properties:

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

b) 将其添加到 application.yaml(或 application.yml):

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
Run Code Online (Sandbox Code Playgroud)


Joã*_*ias 3

我知道这不能直接解决您的问题,但请考虑转向springdoc。Springfox 目前存在如此多的 bug,使用起来很痛苦。由于 Spring WebFlux 支持,我已经转向springdoc2 年前,对此我感到非常高兴。此外,它还支持 Kotlin 协程,我不确定 Springfox 是否支持。

如果您决定迁移,springdoc甚至还有迁移指南