高版本springboot整合swagger3.0

一、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

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值