• Spring Boot整合swagger


    你可能尝试过写完一个接口后,自己去创建接口文档,或者修改接口后修改接口文档。多了之后,你肯定会发生一个操作,那就是忘记了修改文档或者创建文档(除非你们公司把接口文档和写接口要求得很紧密😓忘记写文档就扣工资?,否则两个分离的工作总是有可能遗漏的)。而swagger就是一个在你写接口的时候自动帮你生成接口文档的东西,只要你遵循它的规范并写一些接口的说明注解即可。

    • 自动生成文档,只需要在接口中使用注解进行标注,就能生成对应的接口文档。
    • 自动更新文档,由于是动态生成的,所以如果你修改了接口,文档也会自动对应修改(如果你也更新了注解的话)。这样就不会发送我修改了接口,却忘记更新接口文档的情况。
    • 支持在线调试,swagger提供了在线调用接口的功能。

    前端要接口,写文档

    在springBoot项目中使用

    1、导入依赖

    springboot 版本有时候版本和swagger版本不兼容,但是我测试了一下,我们使用的springboot版本和2.9.12这个是兼容的

    1. <dependency>
    2. <groupId>io.springfox</groupId>
    3. <artifactId>springfox-swagger-ui</artifactId>
    4. <version>2.9.2</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>io.springfox</groupId>
    8. <artifactId>springfox-swagger2</artifactId>
    9. <version>2.9.2</version>
    10. </dependency>

     配置swagger配置类,也可以在springboot配置文件中配置,但是我定义了多个测试环境,就直接定义了自定义配置类

    1. package com.yy.config;
    2. import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.context.annotation.Profile;
    6. import org.springframework.core.env.Environment;
    7. import org.springframework.core.env.Profiles;
    8. import springfox.documentation.builders.ApiInfoBuilder;
    9. import springfox.documentation.builders.PathSelectors;
    10. import springfox.documentation.builders.RequestHandlerSelectors;
    11. import springfox.documentation.service.ApiInfo;
    12. import springfox.documentation.service.Contact;
    13. import springfox.documentation.spi.DocumentationType;
    14. import springfox.documentation.spring.web.plugins.Docket;
    15. import springfox.documentation.swagger2.annotations.EnableSwagger2;
    16. import java.util.ArrayList;
    17. @Configuration
    18. @EnableSwagger2
    19. @EnableSwaggerBootstrapUI // 开启SwaggerBootstrapUI
    20. //@Profile({"dev", "test"}) // 设置swagger的使用环境(防止prod环境api泄露)
    21. public class SwaggerConfig {
    22. @Bean
    23. public Docket docket(Environment environment){
    24. //指定在dev/test环境下使用swagger
    25. Profiles profiles = Profiles.of("dev","test");
    26. System.out.println(profiles);
    27. boolean flag = environment.acceptsProfiles(profiles);
    28. return new Docket(DocumentationType.SWAGGER_2)
    29. .groupName("第一个docker")// 如果配置多个文档的时候,那么需要配置groupName来分组标识
    30. .apiInfo(apiInfo()) // 用于生成API信息
    31. .enable(flag)//关闭swagger,默认是true
    32. .select() //select()函数返回一个ApiSelectorBuilder实例,用来控制接口被swagger做成文档
    33. //RequestHandlerSelectors:配置要扫描的方式,有basePackage("路径")、any():扫描全部,none():全部不扫描
    34. //RequestHandlerSelectors.withMethodAnnotation():扫描方法上的注解
    35. //.withClassAnnotation():扫描类上的注解
    36. .apis(RequestHandlerSelectors.basePackage("com.yy.controller"))//指定扫描的包
    37. //.withClassAnnotation(RestController.class) // 扫描带有指定注解的类下所有接口
    38. //.withMethodAnnotation(PostMapping.class) // 扫描带有指定注解的方法接口
    39. //.apis(RequestHandlerSelectors.any()) // 扫描所有
    40. .paths(PathSelectors.any()
    41. //.any() // 满足条件的路径,该断言总为true
    42. //.none() // 不满足条件的路径,该断言总为false(可用于生成环境屏蔽 swagger)
    43. //.ant("/user/**") // 满足字符串表达式路径
    44. // .regex("") // 符合正则的路径
    45. )//设置请求路径,这里是带有hello的请求路径
    46. .build();
    47. }
    48. private ApiInfo apiInfo(){
    49. //定义作者的姓名,网址,邮箱
    50. Contact contact = new Contact("大米饭", "http://t.csdnimg.cn/zEtOU", "224578908@qq.com");
    51. return new ApiInfoBuilder()
    52. .title("大米的项目API") // 可以用来自定义API的主标题
    53. .description("XX项目SwaggerAPI管理") // 可以用来描述整体的API
    54. .termsOfServiceUrl("https://www.baidu.com") // 用于定义服务的域名(跳转链接)
    55. .version("1.0") // 可以用来定义版本
    56. .license("Swagger-的使用教程")
    57. .licenseUrl("https://blog.csdn.net")
    58. .contact(contact)
    59. .build(); //
    60. }
    61. }

    那既然是记录了接口文档,肯定不能在生产环境中使用 在swagger配置类中配置了在什么环境下可以查看接口及记录的接口文档

    定义了3个环境的配置类,定义使用那个环境的配置类,模拟了一下只配置了端口,生产环境用的8081,在配置类中拿现在环境,进行判断,如果是生产环境关闭swagger 

    定义了两个controller类,用来测试接口

    @ApiOperation(value = "测试方法",notes = "用户测试notes")相当于是一个分组,就像我们用rustful 工具时,先是一个controller类目录,然后,下面有post,get,put方法

     启动启动类

    在浏览器中输入:

    查看没有使用uI的接口文档:http://localhost:8081/swagger-ui.html

     

    @ApiOperation(value = "用户测试",notes = "用户测试notes") 

     使用ui界面需要添加依赖

    在配置文件上开启ui 

    启动启动类

    使用了uI的接口文档:http://localhost:8081/doc.html,即可预览到基于bootstarp的Swagger UI界面 

    他只能提供一个简单的在线调试,如果你想存储你的测试用例,不能像Postman或者YAPI这样保存了你的测试记录

    没有接口文档更新管理,在文档有大的更新的时候可以提前导出一下文档

    参考链接:http://t.csdnimg.cn/KnIVB

  • 相关阅读:
    Android学习笔记 28. Fragement总结
    【图解算法】- 异位词问题:双指针+哈希表
    【区块链 | Compound】2.剖析DeFi借贷产品之Compound:合约篇
    【python】(五)python函数和python匿名函数lambda
    “Python+”集成技术高光谱遥感数据处理
    常见树种(贵州省):005竹类
    【深入Java原子类:高性能并发编程的技巧与实践】
    单目3D目标检测[基于深度辅助篇]
    微服务开发与实战Day01 - MyBatisPlus
    k8s安装3节点集群Fate v1.8.0
  • 原文地址:https://blog.csdn.net/m0_74356429/article/details/140999475