SpringBoot2.6.5配置Swagger3.0(2.9.*不适配)

1.Swagger2.9.*不适配原因

本人首先配置2.9的,但在运行时却会出现Failed to start bean ‘documentationPluginsBootstrapper’的问题,在排查了多方原因后,我发现是springboot的版本更新,导致的swagger2的异常

2.解决思路

2.1暴力解决

看到网上很多都是直接降低springboot版本解决适配问题
缺点:可能导致SpringBoot版本对其余类外部支持产生影响,不建议

2.2使用Swagger3.0版

maven配置:
注意:此处可以添加其他ui依赖,只要能够和swagger依赖共存即可

<!-- Swagger3 -->
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-boot-starter</artifactId>
  <version>3.0.0</version>
</dependency>

添加配置类及参数、注解:

package com.config;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableOpenApi
public class SwaggerConfiguration {
    /**
     * 在完成上述配置之后,其实就已经可以产生帮助文档了,但是这样的文档主要针对请求本身,而描述主要来源于函数等命名产生。
     * 对用户体验不好,我们通常需要自己增加一些说明来丰富文档内容。如果:
     * 加入
     *
     * @ApiIgnore 忽略暴露的 api
     * @ApiOperation(value = "查找", notes = "根据用户 ID 查找用户")
     * 添加说明
     * <p>
     * <p>
     * 其他注解:
     * @Api :用在类上,说明该类的作用
     * @ApiImplicitParams :用在方法上包含一组参数说明
     * @ApiResponses :用于表示一组响应
     * 完成上述之后,启动springboot程序,
     * 旧访问:http://localhost:8080/swagger-ui.html
     * 新访问:http://localhost:8080/doc.html
     * @ApiOperation() 用于方法;表示一个http请求的操作
     * value用于方法描述
     * notes用于提示内容
     * tags可以重新分组(视情况而用)
     * @ApiParam() 用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等)
     * name–参数名
     * value–参数说明
     * required–是否必填
     * @ApiModel()用于类 ;表示对类进行说明,用于参数用实体类接收
     * value–表示对象名
     * description–描述
     * 都可省略
     * @ApiModelProperty()用于方法,字段; 表示对model属性的说明或者数据操作更改
     * value–字段说明
     * name–重写属性名字
     * dataType–重写属性类型
     * required–是否必填
     * example–举例说明
     * hidden–隐藏
     * @ApiIgnore()用于类或者方法上,可以不被swagger显示在页面上 比较简单, 这里不做举例
     * @ApiImplicitParam() 用于方法
     * 表示单独的请求参数
     * @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
     * name–参数ming
     * value–参数说明
     * dataType–数据类型
     * paramType–参数类型
     * example–举例说明
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .select()
                // 核心:读取把那个包下面的方法作为接口,只能是:controller
                .apis(RequestHandlerSelectors.basePackage("com.Controller"))
                .paths(PathSelectors.any())
                .build();
    }


    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title("接口名称")
                .description("数据接口,在线体验文档")
                .termsOfServiceUrl("http://localhost:8088/swagger-ui/index.html#/")
                .version("1.0")
                .build();
    }

}

配置项(yml版本):

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

运行:

地址如下:(端口为项目端口)
http://localhost:8088/swagger-ui/index.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值