• SpringBoot整合Swagger详解


    Swagger详解

    1. 学习目标
    • 了解Swagger的作用和概念
    • 了解前后端分离
    • 在SpringBoot中集成Swagger
    1. Swagger简介
    • 前后端分离

    Vue+SpringBoot

    • 后端时代:

    前端只用管理静态页面–HTML ==> 后端 --模板引擎JSP =>后端是主力

    • 前后端分离时代:

      • 后端:后端控制层、服务层、数据访问层 【后端团队】
      • 前端:前端控制层、视图层 【前端团队】
        • 伪造后端数据,json,已经存在了,不需要后端,前端依旧可以跑起来;但是要给后端交流,给后端一个格式,让后端传数据
    • 前后端如何交互? ===>API

    • 前后端相对独立,松耦合;

    • 前后端甚至可以部署在不同的服务器上;

    产生一个问题:

    • 前后端集成联调,前端人员和后端人员无法做到“及时协商,尽早解决”,最终导致问题集中爆发

    解决方案:

    • 首先指定一个schema(计划的提纲),实时更新最新API,降低集成的风险
    • 早些年:指定word计划文档;
    • 现在,前后端分离:
      • 前端测试后端接口:postman 早些年用
      • 后端提供接口,需要实时更新最新的消息及改动!
    • Swagger-----“号称世界上最流行的API框架”
      • RestFul Api 文档在线自动生成工具==>Api文档与API定义同步更新
      • 直接运行,可以在线测试API接口
      • 支持多种语言:Java,php
      • 官网:https://swagger.io/
    1. 在项目中使用Swagger
    • 需要springfox 依赖
      • swagger2
      • ui
    1. SpringBoot集成Swagger
    • 新建一个springbot项目

    • 导入相关依赖

    
    <dependency>
        <groupId>io.springfoxgroupId>
        <artifactId>springfox-swagger2artifactId>
        <version>3.0.0version>
    dependency>
    
    
    <dependency>
        <groupId>io.springfoxgroupId>
        <artifactId>springfox-swagger-uiartifactId>
        <version>3.0.0version>
    dependency>
    
    
    3.0也可以直接用启动器,springboot
    
    <dependency>
        <groupId>io.springfoxgroupId>
        <artifactId>springfox-boot-starterartifactId>
        <version>3.0.0version>
    dependency>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    ps:最好用上面两个依赖,启动器整合的可能会出现bug

    • 配置swagger–config
    package com.qian.swagger.config;
    
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2  //开启Swagger2
    public class SwaggerConfig {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 写一个controller,测试
    package com.qian.swagger.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController  //返回字符串
    public class HelloController {
    
        @RequestMapping(value = "/hello")
        public String hello(){
            return "hello";
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 进入http://localhost:8080/swagger-ui.html 进入swagger

      • 如果进入不了,则将依赖降级,例如降到2.9.2,可以成功进入
    • 因为版本问题启动报错,可以在配置文件上加上

      ​ - spring.mvc.pathmatch.matching-strategy=ant_path_matcher

    1. 配置swagger
    • Swagger的bean实例 Docket
    package com.qian.swagger.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    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 javax.xml.bind.annotation.XmlType;
    import java.util.ArrayList;
    
    @Configuration
    @EnableSwagger2  //开启Swagger2
    public class SwaggerConfig {
        //配置了Swagger的Docket的bean实例
        @Bean
        public Docket docket(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo());
    
        }
        //配置Swagger信息==apiInfo
        private ApiInfo apiInfo(){
            Contact contact = new Contact("xqh","https://blog.csdn.net/m0_56116754?spm=1000.2115.3001.5343","791172229@qq.com");
            return new ApiInfo(
                    "我的SwaggerApi文档",
                    "即使最小的帆也能远航",
                    "1.0",
                    "https://blog.csdn.net/m0_56116754?spm=1000.2115.3001.5343",
                    contact,
                    "Apache 2.0",
                    "http://apache.org/licenses/LICENSE-2.0",
                    new ArrayList());
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • Swagger配置扫描接口

    Docket.select()

     @Bean
        public Docket docket(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    //RequestHandlerSelectors 配置要扫描的接口的方法
                    //basePackage:指定要扫描的包
                    //any():扫描全部
                    //none():都不扫描
                    //withClassAnnotation :扫描类上的注解
                    //withMethodAnnotation:扫描方法上的注解
                    .apis(RequestHandlerSelectors.basePackage("com.qian.swagger.controller"))
                    //过滤什么路径
                    //ant():只扫描
                    //any():过滤全部
                    //none():都不过滤
                    .paths(PathSelectors.none())
                    .build();
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    配置是否启动swagger

     .enable(false)  //不能使用swagger
    
    • 1

    😱 Could not render e, see the console.

    • 题目:我只希望我的swagger在生产环境中使用,在发布的时候不使用,该怎么做?
      • 判断是不是生产环境
      • 注入enable()

    1)判断生产环境

       //设置要显示的Swagger环境
            Profiles profiles=Profiles.of("dev","test");
    
            //获取项目环境:通过environment.acceptsProfiles判断是否处在自己设定的环境当中
            boolean flag = environment.acceptsProfiles(profiles);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    //系统检测到是dev环境或者test环境,就可以启动swagger

    2)多环境配置

    applicatin-dev.properties

    server.port=8081
    
    • 1

    application-test.properties

    server.port=8082
    
    • 1

    application.properties

    spring.mvc.pathmatch.matching-strategy=ant_path_matcher
    
    spring.profiles.active=dev
    
    • 1
    • 2
    • 3

    //启用的是dev环境,之前设置的是检测到dev环境就可以启动swagger,所以进入http://localhost:8080/swagger-ui.html 可以进入swagger 。这样就能实现只在生产环境启动swagger

    • 配置API文档的分组
    .groupName("xqh")
    
    • 1

    配置多个分组

      @Bean
        public Docket docket1(){
            return new Docket(DocumentationType.SWAGGER_2).groupName("A")
        }
        @Bean
        public Docket docket2(){
            return new Docket(DocumentationType.SWAGGER_2).groupName("B")
        }
        @Bean
        public Docket docket3(){
            return new Docket(DocumentationType.SWAGGER_2).groupName("C")
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 一些注释作用

    @ApiModel 给实体类加上注释

    @ApiModel("用户实体类")
    public class User {
        @ApiModelProperty("用户名")
        public String username;
        @ApiModelProperty("密码")
        public String pwd;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    @Api(tags = “控制器”)给控制类加注释

    @ApiOperation 给方法加注释

    @ApiParam给参数加注释

    package com.qian.swagger.controller;
    
    import com.qian.swagger.pojo.User;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    @Api(tags = "控制器")
    @RestController  //返回字符串
    public class HelloController {
    
        @GetMapping(value = "/hello")
        public String hello(){
            return "hello";
        }
        //只要我们的接口中,返回值中存在实体类,就会被扫描到swagger中
        @PostMapping(value = "/user")
        public User user(){
            return new User();
        }
    
        //Operation接口,不是放在类上,是方法。给方法加上注释
        @ApiOperation("hello2控制")
        @GetMapping("/hello2")
        public String hello2(@ApiParam("用户名") String username){
            return "hello"+username;
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    1. 总结
    • 我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息
    • 接口文档实时更新
    • 可以在线测试
    • 前后端有效交流

    Swagger是一个优秀的工具,几乎所有大公司都有使用它。

    注意点:

    在正式发布的时候,关闭Swagger!出于安全考虑,并且节省运行的内存。

  • 相关阅读:
    IPD各阶段交付文档
    Jmeter(七):jmeter连接数据库/中元件的执行顺序&作用域详解
    数字图像处理笔记(二)图像增强-直方图修改技术
    100 Gbps 网卡的 TCP 困境
    【管理运筹学】第 7 章 | 图与网络分析(4,最大流问题)
    Rsync学习笔记1
    【华为OD机试真题 python】 5键键盘【2022 Q4 | 100分】
    新考纲下的PMP考试有多难?全面解析
    Point(类与构造) Java
    springboot社工服务中心管理信息系统毕业设计源码021009
  • 原文地址:https://blog.csdn.net/m0_56116754/article/details/126182947