1、Springboot集成使用swagger2

1.1导入相关依赖

  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>

1.2配置

在config.SwaggerConfig下配置swagger

  1. import org.springframework.context.annotation.Configuration;
  2. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  3. /**
  4. * @author cyx
  5. * @create 2021-01-11 11:12
  6. */
  7. @Configuration
  8. @EnableSwagger2 //开启swagger2
  9. public class SwaggerConfig {
  10. }

1.3测试访问

http://localhost:8080/swagger-ui.html
20201012154143442.png

2、配置Swagger信息

  1. package com.kuang.swagger.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.service.ApiInfo;
  6. import springfox.documentation.service.Contact;
  7. import springfox.documentation.spi.DocumentationType;
  8. import springfox.documentation.spring.web.plugins.Docket;
  9. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  10. @Configuration
  11. @EnableSwagger2 //开启Swagger2
  12. public class SwaggerConfig {
  13. @Bean
  14. public Docket createRestApi(){
  15. return new Docket(DocumentationType.SWAGGER_2)
  16. .apiInfo(apiInfo());
  17. }
  18. //配置Swagger 信息=apiInfo
  19. private ApiInfo apiInfo() {
  20. //作者信息
  21. Contact contact = new Contact("cyx","1","807357194@qq.com");
  22. return new ApiInfoBuilder()
  23. .title("cyx的SwaggerAPI文档")
  24. .description("Swagger的API配置")
  25. .termsOfServiceUrl("https://blog.csdn.net/gjs935219")
  26. .contact(contact)
  27. .version("v1.0")
  28. .build();
  29. }

Swagger配置扫描接口

  1. @Bean
  2. public Docket docket(){
  3. Docket build = new Docket(DocumentationType.SWAGGER_2)
  4. .apiInfo(apiInfo())
  5. .select()
  6. //RequestHandlerSelectors:配置要扫描接口的方式
  7. //basePackage:指定要扫描的包
  8. //any():扫描全部
  9. //none():不扫描
  10. //withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象
  11. //withMethodAnnotation:扫描方法上的注解
  12. .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
  13. //path():过滤什么路径
  14. .paths(PathSelectors.ant("/cyx/**"))
  15. .build(); return build;
  16. }

配置是否启动swagger

  1. //配置了Swagger的docket的bean实例
  2. @Bean
  3. public Docket docket(){
  4. Docket build = new Docket(DocumentationType.SWAGGER_2)
  5. .apiInfo(apiInfo())
  6. .enable(false)//enable是否启动swagger,如果为false,则swagger不能在浏览器中访问
  7. .select()
  8. .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
  9. // .paths(PathSelectors.ant("/cyx/**"))
  10. .build(); return build;
  11. }

只希望Swagger在生产环境中使用,在发布的时候不使用???

  • 判断是不是生产环境,flag=false
  • 注入enable(flag)

一个开发环境,一个开发完成后发布的环境
20201012164004878.png
2020101216402533.png
20201012164039105.png

  1. package com.cyx.swaggerdemo.config;
  2. import com.cyx.swaggerdemo.controller.HelloController;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.core.env.Environment;
  6. import org.springframework.core.env.Profiles;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import springfox.documentation.builders.ApiInfoBuilder;
  10. import springfox.documentation.builders.PathSelectors;
  11. import springfox.documentation.builders.RequestHandlerSelectors;
  12. import springfox.documentation.service.ApiInfo;
  13. import springfox.documentation.service.Contact;
  14. import springfox.documentation.spi.DocumentationType;
  15. import springfox.documentation.spring.web.plugins.Docket;
  16. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  17. /**
  18. * @author cyx
  19. * @create 2021-01-11 11:12
  20. */
  21. @Configuration
  22. @EnableSwagger2 //开启swagger2
  23. public class SwaggerConfig {
  24. //配置了Swagger的docket的bean实例
  25. @Bean
  26. public Docket docket(Environment environment){
  27. //设置要显示的Swagger环境
  28. Profiles profiles = Profiles.of("dev","test");
  29. //通过environment.acceptsProfiles判断是否处在自己设定的环境中
  30. boolean flag = environment.acceptsProfiles(profiles);
  31. Docket build = new Docket(DocumentationType.SWAGGER_2)
  32. .apiInfo(apiInfo())
  33. .enable(flag)//enable是否启动swagger,如果为false,则swagger不能在浏览器中访问
  34. .select()
  35. .apis(RequestHandlerSelectors.basePackage("com.cyx.swaggerdemo.controller"))
  36. // .paths(PathSelectors.ant("/cyx/**"))
  37. .build(); return build;
  38. }
  39. //配置Swagger信息=apiinfo
  40. private ApiInfo apiInfo(){
  41. //作者信息
  42. Contact contact = new Contact("cyx","1","807357194@qq.com");
  43. return new ApiInfoBuilder()
  44. .title("cyx的SwaggerAPI文档")
  45. .description("服务名:springcloudproducer")
  46. .version("1.0")
  47. .build();
  48. }
  49. }

配置API文档的分组

  1. Docket(DocumentationType.SWAGGER_2)
  2. .groupName("cyx")

如何配置多个分组:多个Docket实例即可

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

实体类配置

  1. package com.cyx.swaggerdemo.pojo;
  2. import io.swagger.annotations.Api;
  3. import io.swagger.annotations.ApiModel;
  4. import io.swagger.annotations.ApiModelProperty;
  5. /**
  6. * @author cyx
  7. * @create 2021-01-11 14:06
  8. */
  9. //@Api(注释)
  10. @ApiModel("用户实体类")
  11. public class User {
  12. @ApiModelProperty("用户名")
  13. public String username;
  14. @ApiModelProperty("密码")
  15. public String password;
  16. }

Controller配置

  1. package com.cyx.swaggerdemo.controller;
  2. import com.cyx.swaggerdemo.pojo.User;
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiOperation;
  5. import io.swagger.annotations.ApiParam;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. /**
  11. * @author cyx
  12. * @create 2021-01-11 11:09
  13. */
  14. @Api(tags = "这是测试")
  15. @RestController
  16. public class HelloController {
  17. @GetMapping(value = "/hello")
  18. public String hello(){
  19. return "hello";
  20. }
  21. //只要我们的接口中,返回值中存在实体类,他就会被扫描到swagger中
  22. @PostMapping(value = "/user")
  23. public User user(){
  24. return new User();
  25. }
  26. @ApiOperation("hello控制类")
  27. @GetMapping(value = "/hello2")
  28. public String hello2(@ApiParam("用户名") String username){
  29. return "hello" + username;
  30. }
  31. }

注释

作用范围 API 使用位置

对象属性 @ApiModelProperty 用在参数对象的字段上

协议集描述 @Api 用在Conntroller类上

协议描述 @ApiOperation 用在controller方法上

Response集 @ApiResponses 用在controller方法上

Response @ApiResponse 用在@ApiResponses里面

非对象参数集 @ApilmplicitParams 用在controller方法上

总结

1、我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息

2、接口文档实时更新

3、可以在线测试

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

【注意点】在正式发布的时候,关闭Swagger!!!出于安全考虑,而且节省运行的内存!!!