> 文档中心 > Swagger3使用的基本步骤,看了就是会了

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@Configurationpublic 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")@RestControllerpublic 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.
swagger2 OpenAPI3 注解位置
@Api @Tag(name = “接口类描述”) Controller 类上
@ApiOperation @Operation(summary =“接口方法描述”) Controller 方法上
@ApiImplicitParams @Parameters Controller 方法上
@ApiImplicitParam @Parameter(description=“参数描述”) Controller 方法上 @Parameters里
@ApiParam @Parameter(description=“参数描述”) Controller 方法的参数上
@ApiIgnore @Parameter(hidden = true) 或 @Operation(hidden = true) 或 @Hidden
@ApiModel @Schema DTO类上
@ApiModelProperty @Schema DTO属性上

请添加图片描述