swagger2启动报错

发布时间 2023-10-09 19:28:43作者: 新游

1.添加注解:@EnableSwagger2

注解能将swagger的配置文件导入ioc容器,使用容器管理swagger相关的各种核心类,并使我们能通过yml或properties文件能修改默认配置:

@Import({Swagger2DocumentationConfiguration.class})
public @interface EnableSwagger2 {}
...

@EnableSwagger2能添加在springboot启动类上,也可以添加在自定义swagger配置类上

@Configuration //配置类
@EnableSwagger2
public class SwaggerConfig {}

2.修改swagger2的路径匹配法则

SpringBoot2.6.x使用PathPatternMatcher匹配路径,Swagger引用的Springfox基于AntPathMatcher匹配路径。匹配方式不同,会导致启动报错空指针。

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

修改完后启动访问:http://ip:port/swagger-ui.html#/即可访问swagger页面