SpringBoot学习(七)————Swagger、集成swagger、swagger配置扫描接口、配置API

Swagger

现如今是前后端分离的时代,为了解决前后端之间沟通之间的矛盾,可以使用swagger,进行接口的实时更新

RestFul风格文档在线自动生成工具=》API文档和API定义同步更新

直接运行,可以在线测试API接口

在项目使用swagger需要springbox

  • swagger2
  • ui

集成swagger

导包

<!--        swagger   使用2.9.2版本,对于3.0.0没有seagger-ui.html-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>

配置文件

@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {

}

测试访问url:http://localhost:8080/swagger-ui.html

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MTNcFb4Z-1627979404923)(SpringBoot.assets/image-20210803133259204.png)]

swagger页面设置

@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {

    //创建bean
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());//设置api页面
    }

    //配置swagger信息
    public ApiInfo apiInfo() {
        Contact contact = new Contact("wjq", "localhost", "111111@QQ.com");
        return new ApiInfo(
                "MyAPI文档"
                ,"种树人"
                ,"w1.0"
                ,"localhost:8080"
                ,contact
                ,"Apache 2.0"
                ,"http://locense"
                ,new ArrayList());
    }
}

swagger配置扫描接口

SwaggerConfig

@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {

    //创建bean
    @Bean
    public Docket docket(Environment environment) {
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev", "test");
        //通过environment.acceptsProfiles判断是否在自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);
        System.err.println("flag--->" + flag);

        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            //是否启动swagger,默认为true,改为false则没有启动
            .enable(true)//可以将flag注入,当在允许的环境中(返回true),即可以使用
            .select()//select()之后只能调用path()/build()/apis()
            /*
             *RequestHandlerSelectors配置要扫描接口的方式
             *any()扫描全部
             *none()不扫描
             *basePackage("com.wjq.controller")指定要扫描的包
             *withClassAnnotation()扫描类上的注解   参数是注解的对象
             * withMethodAnnotation()扫描方法上的注解   参数是注解的对象
             */
            .apis(RequestHandlerSelectors.basePackage("com.wjq.controller"))
            //paths()过滤什么路径,只扫描/wjq url下的路径
//            .paths(PathSelectors.ant("/wjq/**"))
            .build();//设置api页面
    }

    //配置swagger信息
    public ApiInfo apiInfo() {
        Contact contact = new Contact("wjq", "localhost", "111111@QQ.com");
        return new ApiInfo(
                "MyAPI文档"
                ,"种树人"
                ,"w1.0"
                ,"localhost:8080"
                ,contact
                ,"Apache 2.0"
                ,"http://locense"
                ,new ArrayList());
    }
}

application.yml

spring:
  profiles:
    active: dev

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uSZLUTlf-1627979404925)(SpringBoot.assets/image-20210803145505343.png)]
在dev和pro环境中设置不同的port

配置API文档的分组

SwaggerConfig

@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {

    //创建多个Docket
    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("AA");
    }
    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("BB");
    }
    @Bean
    public Docket docket3() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("CC");
    }
    
    //创建bean
    @Bean
    public Docket docket(Environment environment) {
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev", "test");
        //通过environment.acceptsProfiles判断是否在自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);
        System.err.println("flag--->" + flag);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("wjq")
                //是否启动swagger,默认为true,改为false则没有启动
                .enable(true)//可以将flag注入,当在允许的环境中(返回true),即可以使用
                .select()//select()之后只能调用path()/build()/apis()
                /*
                 *RequestHandlerSelectors配置要扫描接口的方式
                 *any()扫描全部
                 *none()不扫描
                 *basePackage("com.wjq.controller")指定要扫描的包
                 *withClassAnnotation()扫描类上的注解   参数是注解的对象
                 * withMethodAnnotation()扫描方法上的注解   参数是注解的对象
                 */
                .apis(RequestHandlerSelectors.basePackage("com.wjq.controller"))
                //paths()过滤什么路径,只扫描/wjq url下的路径
    //            .paths(PathSelectors.ant("/wjq/**"))
                .build();//设置api页面
    }

    //配置swagger信息
    public ApiInfo apiInfo() {
        Contact contact = new Contact("wjq", "localhost", "111111@QQ.com");
        return new ApiInfo(
                "MyAPI文档"
                ,"种树人"
                ,"w1.0"
                ,"localhost:8080"
                ,contact
                ,"Apache 2.0"
                ,"http://locense"
                ,new ArrayList());
    }
}

相关注解【感觉就是用来起别名的】

  • @ApiModel() Model类上
  • @ApiModelProperty() 属性上
  • @ApiOperation() 方法上
  • @ApiParam() 参数上
@RestController
public class MyController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    //只要我们的接口中,返回值中存在实体类,就会被swagger扫描到
    @PostMapping("/user")
    public User user() {
        return new User();
    }

    @ApiOperation("hello控制类")
    @GetMapping("hello1")
    public String hello(@ApiParam("用户名") String username) {
        return "hello" + username;
    }

    @ApiOperation("post控制类")
    @PostMapping("postt")
    public User postt(@ApiParam("用户") User user) {
        int i = 5 / 0;//在swagger中测试时,此处会报错
        return new User();
    }
}

User

@ApiModel("用户实体类")
public class User {
    //与这些注解无关,注释只是为了方便阅览
    @ApiModelProperty("用户名")
    public String username;//如果是private则在页面中不可见
    @ApiModelProperty("密码")
    public String password;
}

测试报错

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OHPIajAU-1627979404926)(SpringBoot.assets/image-20210803160116050.png)]

总结:

  1. 我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息
  2. 接口文档实时更新
  3. 可以在线测试

注意点:在正是发布的时候,关闭Swagger!出于安全考虑。而且节省允许的内存


版权声明:本文为weixin_45734378原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。