Swagger的简单使用

一、使用步骤:

1.导入依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

2.自定义swagger的配置

新建一个配置类:

package com.gk.myswagger.config;

import com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwapperConfig {
    @Bean  //配置swagger的bean实例   Environment可以获取当前项目环境:开发环境、测试环境
    public Docket getDocket(Environment environment){

        //设定要查看的项目环境:dev   可以设定多个,后面用逗号分开
        Profiles profiles = Profiles.of("dev");
        //查看当前项目环境是否处于自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);

        //作者信息
        Contact contact = new Contact("拔魔", "https://www.cnblogs.com/gekun154/", "2249843344@qq.com");
        ApiInfo apiInfo = new ApiInfo(
                "拔魔的swapper",
                "这是第一次创建",
                "V1.0",
                "https://www.cnblogs.com/gekun154/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                //enable 是否开启swagger  flag:表示当前的项目环境是否是自己预先设定好的(如果当前项目环境是dev就开启swagger)
                .enable(flag)
                //通过select() + ... + build() 可以配置扫描接口信息
                .select()
                //RequestHandlerSelectors.basePackage("包名") 设置基于包进行扫描 常用
                //RequestHandlerSelectors.any() 设置扫描所有的包
                //RequestHandlerSelectors.withClassAnnotation(注解名字.class) 设置通过类上面的接口进行扫描
                //RequestHandlerSelectors.withMethodAnnotation(注解名字.class) 设置通过方法上面的接口进行扫描
                .apis(RequestHandlerSelectors.basePackage("com.gk.myswagger.controller"))
                //paths 过滤哪个路径
                //PathSelectors.ant("路径名")  过滤以某路径开头的请求 例如:路径名为:/gk/** === 过滤以gk开头的所有请求,除了以gk开头请求,其他请求全过滤掉。
                //PathSelectors.regex("/.*")通过正则来过滤请求路径
                .paths(PathSelectors.regex("/.*"))
//                .paths(PathSelectors.ant("/gk/**"))
                .build();
    }

}

3.访问

http://localhost:8081/swagger-ui.html

4.swagger注释书写

  • @ApiModel("注释名") 给当前类添加注释
  • @ApiModelProperty("注释名") 给当前类的属性添加注释
  • @ApiOperation("注释类容") 给当前方法添加注释
  • @ApiParam("注释类容") 给当前参数添加注释

示例

//java bean中
@ApiModel("用户实体类")//给当前类添加注释
public class Empty {
    @ApiModelProperty("用户名")//给当前属性添加注释
    private String name;
    private String pwd;
}
//controller层中
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
    @GetMapping("gk/hello")
    @ApiOperation("这是一个show控制层")//给这个方法写注释
    //ApiParam:给参数写注释
    public String show(@ApiParam("传入一个arg字符串") String arg){
        return "hello world";
    }
}