一、Swagger介绍
swagger是前后端分离的产物,工作中后端开发的接口需要写接口文档交给前端,前段按接口文档开发页面,因为写接口文档费事费力,干脆搞个自动的接口文档,这就是swagger
二、环境
springboot版本: 2.7.6
swagger版本: 3.0
二、springboot整合swagger3.0
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<spring-boot.version>2.7.6</spring-boot.version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
配置
application.yml配置
spring:
mvc:
pathmatch:
#swagger3.0与高版本springboot不兼容解决办法
matching-strategy: ant_path_matcher
启动类配置
//swagger注解
@EnableOpenApi
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
配置类配置
由于Swagger不更新了,导致其默认配置下对于高版本的Spring Boot的不兼容
原因是Spring MVC更换了默认的路径匹配策略
由MatchingStrategy.ANT_PATH_MATCHER替换MatchingStrategy.PATH_PATTERN_PARSER
如果不改就会报Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
第一种方法:实现BeanPostProcessor的postProcessBeforeInitialization方法将WebMvcEndpointHandlerMapping的patternParser属性配置为null
@Configuration
@EnableSwagger2
public class SwaggerConfig implements BeanPostProcessor {
//swagger3.0与高版本springboot不兼容解决办法,配置WebMvcEndpointHandlerMapping的patternParser为null
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean.getClass().equals(WebMvcEndpointHandlerMapping.class)) {
((WebMvcEndpointHandlerMapping) bean).setPatternParser(null);
}
return bean;
}
}
第二个方法:新建一个WebMvcEndpointHandlerMapping覆盖原类,配置patternParser属性为null
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//swagger3.0与高版本springboot不兼容解决办法,配置WebMvcEndpointHandlerMapping的patternParser为null
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier,
EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
WebEndpointProperties webEndpointProperties, Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping =
webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath)
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
shouldRegisterLinksMapping, null);
}
}
访问swagger
URL:http://localhost:XXXX/XX/swagger-ui/index.html
XXXX:是你项目端口号
XX:是你项目的上下文路径
如果你是8080端口并且没配置上下文路径请点 http://localhost:8080/swagger-ui/index.html