Swagger3使用的基本步骤,看了就是会了

Swagger3使用的基本步骤,看了就是会了

了解swagger的作用和概念

世界上最流行的API框架

RestfulAPi 文档在线自动生成工具= API文档与API定义同步更新
直接运行,可以在线测试Api接口

使用基本步骤

  1. 导入使用swagger的一些依赖
		<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
  1. 配置application.yml文件
spring:
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER
  1. 编写Swagger3的配置类
package com.example.mybatistest.config;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@EnableOpenApi
@Configuration
public class swaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket createRestApi() {
        //返回文档摘要信息
        return new Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo())
            .select()
            //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            //配合@EnableOpenApi 找到API位置,不需要再在启动类上配置
            .apis(RequestHandlerSelectors.basePackage("com.example.mybatistest.controller"))
            .paths(PathSelectors.any())
            .build();

    }

    //生成接口信息,包括标题、联系人等
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("Swagger3接口文档")
            .description("Rabbit用Swagger3.0。")
            .contact(new Contact("Rabbit", "https://swagger.io/", "362250024@qq.com"))
            .version("1.0")
            .build();
    }
    
}
  1. 编写对应的controller
package com.example.mybatistest.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "控制器--Hello")
@RequestMapping("/hello")
@RestController
public class testSwagger3 {
    
    @GetMapping("/getAll")
    @ApiOperation("查询方法--getAll")
    public String getAll() {
        return "getAll";
    }
}
  1. 默认地址

    http://localhost:8080/swagger-ui/index.html#/

  2. 使用swagger一些相关的注解

    1. springfox对旧版的 swagger做了兼容处理。 所以在swagger3中可以继续使用swagger2的注解。
    2. 注意修改 swagger 3 注解的包路径为io.swagger.v3.oas.annotations.
swagger2OpenAPI3注解位置
@Api@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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Rabbityyhh

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

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

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

打赏作者

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

抵扣说明:

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

余额充值