集成Swagger

  • 前后端分离


前端 -> 前端控制层、视图层
后端 -> 后端控制层、服务层、数据访问层
前后端通过API进行交互
前后端相对独立且松耦合

产生的问题


前后端集成,前端或者后端无法做到“及时协商,尽早解决”,最终导致问题集中爆发

解决方案


首先定义schema [ 计划的提纲 ],并实时跟踪最新的API,降低集成风险

Swagger


号称世界上最流行的API框架
Restful Api 文档在线自动生成器 => API 文档 与API 定义同步更新
直接运行,在线测试API
支持多种语言 (如:Java,PHP等)
官网:https://swagger.io/

SpringBoot集成Swagger


SpringBoot集成Swagger => springfox,两个jar包

  • Springfox-swagger2

  • swagger-springmvc

  • 使用Swagger
    要求:jdk 1.8 + 否则swagger2无法运行
    步骤:
    1、新建一个SpringBoot-web项目
    2、添加Maven依赖

  1. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.9.2</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
  8. <dependency>
  9. <groupId>io.springfox</groupId>
  10. <artifactId>springfox-swagger-ui</artifactId>
  11. <version>2.9.2</version>
  12. </dependency>

编写HelloController,测试确保运行成功!

4、要使用Swagger,我们需要编写一个配置类-SwaggerConfig来配置 Swagger

  1. package com.daoyan.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.service.ApiInfo;
  5. import springfox.documentation.service.Contact;
  6. import springfox.documentation.spi.DocumentationType;
  7. import springfox.documentation.spring.web.plugins.Docket;
  8. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  9. import java.util.ArrayList;
  10. @Configuration
  11. @EnableSwagger2
  12. public class SwaggerConfig {
  13. //配置了Swagger的Docket的bean实例
  14. @Bean
  15. public Docket docket(){
  16. return new Docket(DocumentationType.SWAGGER_2)
  17. .apiInfo(apiInfo());
  18. }
  19. //配置Swagger信息=apiInfo
  20. private ApiInfo apiInfo(){
  21. //作者信息
  22. Contact contact = new Contact("刘霈霖","https://blog.csdn.net/weixin_40928238?assign_skin_id=77","2878612610@qq.com");
  23. return new ApiInfo("刘爷的SwaggerAPI文档",
  24. "即使再小的帆也能远航",
  25. "V1.0",
  26. "https://blog.csdn.net/weixin_40928238?assign_skin_id=77",
  27. contact,
  28. "Apache 2.0",
  29. "http://www.apache.org/licenses/LICENSE-2.0",
  30. new ArrayList());
  31. }
  32. }

5、访问测试 :http://localhost:8080/swagger-ui.html ,可以看到swagger的界面;

配置扫描接口

1、构建Docket时通过select()方法配置怎么扫描接口。

  1. @Bean
  2. public Docket docket() {
  3. return new Docket(DocumentationType.SWAGGER_2)
  4. .apiInfo(apiInfo())
  5. .select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
  6. .apis(RequestHandlerSelectors.basePackage("com.kuang.swagger.controller"))
  7. .build();
  8. }

2、重启项目测试,由于我们配置根据包的路径扫描接口,所以我们只能看到一个类

3、除了通过包路径配置扫描接口外,还可以通过配置其他方式扫描接口,这里注释一下所有的配置方式:

  1. any() // 扫描所有,项目中的所有接口都会被扫描到
  2. none() // 不扫描接口
  3. // 通过方法上的注解扫描,如withMethodAnnotation(GetMapping.class)只扫描get请求
  4. withMethodAnnotation(final Class<? extends Annotation> annotation)
  5. // 通过类上的注解扫描,如.withClassAnnotation(Controller.class)只扫描有controller注解的类中的接口
  6. withClassAnnotation(final Class<? extends Annotation> annotation)
  7. basePackage(final String basePackage) // 根据包路径扫描接口

4、除此之外,我们还可以配置接口扫描过滤:

  1. @Bean
  2. public Docket docket() {
  3. return new Docket(DocumentationType.SWAGGER_2)
  4. .apiInfo(apiInfo())
  5. .select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
  6. .apis(RequestHandlerSelectors.basePackage("com.kuang.swagger.controller"))
  7. // 配置如何通过path过滤,即这里只扫描请求以/kuang开头的接口
  8. .paths(PathSelectors.ant("/kuang/**"))
  9. .build();
  10. }

5、这里的可选值还有

  1. any() // 任何请求都扫描
  2. none() // 任何请求都不扫描
  3. regex(final String pathRegex) // 通过正则表达式控制
  4. ant(final String antPattern) // 通过ant()控制