springboot项目使用Swagger3

11 篇文章 0 订阅
2 篇文章 0 订阅

一、Swagger介绍

  • 号称世界上最流行的Api框架;
  • Restful Api 文档在线自动生成工具=>Api文档与API定义同步更新
  • 直接运行,可以在在线测试API 接口
  • 支持多种语言:(java,Php…)

二、Swagger3 准备工作

1、在pom.xml导入依赖
<!--        Swagger3-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

注:导入依赖运行报错,可在配置中添加

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
2、配置类

访问地址:

文档接口地址:http://localhost:9990/v3/api-docs
文档页面地址:http://localhost:9990/swagger-ui/index.html

注意:需要在配置上需要释放Swagger被拦截器拦截的路径,如

"/swagger-resources/**"
,"/webjars/**"
,"/swagger-ui/**"
,"/v3/**"
,"/swagger-ui.html/**"

在这里插入图片描述
我们可以通过配置类修改接口文档UI的内容:

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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;

/**
* Swagger3配置类
* */
@Configuration
@EnableOpenApi
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo())
            .select()
            /**加了@ApiOperation注解的类,才生成接口文档*/
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            /**显示所有路径*/
            .paths(PathSelectors.any())
            .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("XX接口文档")
            .description("XX接口文档XXXX")
            .version("1.0")
            .build();
    }

}

修改之后接口文档的页面:

在这里插入图片描述

3、Swagger的基本使用
swagger 2OpenAPI 3注解位置
@Api(tags = “接口类描述”)@Tag(name = “接口类描述”)Controller 类上
@ApiOperation@Operation(summary =“接口方法描述”)Controller 方法上
@ApiImplicitParams@ParametersController 方法上
@ApiImplicitParam@Parameter(description=“参数描述”)Controller 方法上 @Parameters 里
@ApiParam@Parameter(description=“参数描述”)Controller 方法的参数上
@ApiIgnore@Parameter(hidden = true) 或 @Operation(hidden = true) 或 @Hidden可以用在类、方法上,方法参数中,用来屏蔽某些接口或参数,使其不在页面上显示
@ApiModel@SchemaDTO类上
@ApiModelProperty@SchemaDTO属性上
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙卷风爱卷

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值