Springboot 2.6.2整合swagger2报错问题

我们经常使用这个swagger2来测试接口,swagger2可以快速的编写API接口文档,但是使用这个swagger2的时候,与springboot整合时,有可能会出现问题。我就遇上了这样一个问题,我使用的是Spring Boot Starter Swagger的依赖:

<!-- https://mvnrepository.com/artifact/com.spring4all/swagger-spring-boot-starter -->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>

在配置文件中,配置也是没有任何的问题

#swagger配置
swagger:
  base-package: com.wang.demo01.member.controller
  title: "会员模块-swagger"
  description: "描述"
  version: "2.0"
  contact:
    name: "test"
    email: "test@163.com"
    url: "https://www.baidu.com"
  terms-of-service-url: "服务条款:https://www.baidu.com"

启动类上加上这个@EnableSwagger2

@SpringBootApplication
@MapperScan("com.wang.demo01.member.dao")
@EnableSwagger2
public class Demo01Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo01Application.class, args);
    }

}

总体上看似乎没有问题,但是启动的时候,报了以下的错误

因为这个错误,找了许久也浪费了好多时间,最后发现是springboot与swagger发生版本不兼容的问题。主要有两个解决的方案。

方案一:在xxx.yml文件中

加上:

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

如果是xxx.properties文件中加上:

spring: mvc: pathmatch: matching-strategy: ANT_PATH_MATCHER

方案二:springboot降级

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
经验分享 程序员 微信小程序 职场和发展