就是api文档

官网地址

导入依赖

  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>

配置Swagger

  1. package com.lyd.swagger.config;
  2. import org.springframework.context.annotation.Configuration;
  3. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  4. @Configuration
  5. @EnableSwagger2 //启动swagger2
  6. public class SwaggerConfig {
  7. }

启动访问Swagger页面

http://localhost:8080/swagger-ui.html
如果启动报错,可能需要把boot版本降到2.5.1。
image.png

配置Swagger基本信息

  1. package com.lyd.swagger.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 //启动swagger2
  12. public class SwaggerConfig {
  13. //配置了Swagger的Docket的bean实例
  14. @Bean
  15. public Docket docket(){
  16. //看源码,Docket里面很多属性
  17. return new Docket(DocumentationType.SWAGGER_2)
  18. .apiInfo(apiInfo());
  19. }
  20. //配置swagger自定义信息
  21. private ApiInfo apiInfo(){
  22. return new ApiInfo(
  23. "API文档",
  24. "这是测试学习",
  25. "1.0",
  26. "urn:tos",
  27. new Contact("lyd","lyd.com","206977438@qq.com"),
  28. "Apache 2.0",
  29. "http://www.apache.org/licenses/LICENSE-2.0",
  30. new ArrayList());
  31. }
  32. }

image.png

配置Swager扫描接口

  1. package com.lyd.swagger.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import springfox.documentation.builders.PathSelectors;
  6. import springfox.documentation.builders.RequestHandlerSelectors;
  7. import springfox.documentation.service.ApiInfo;
  8. import springfox.documentation.service.Contact;
  9. import springfox.documentation.spi.DocumentationType;
  10. import springfox.documentation.spring.web.plugins.Docket;
  11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  12. import java.util.ArrayList;
  13. @Configuration
  14. @EnableSwagger2 //启动swagger2
  15. public class SwaggerConfig {
  16. //配置了Swagger的Docket的bean实例
  17. @Bean
  18. public Docket docket(){
  19. //看源码,Docket里面很多属性
  20. return new Docket(DocumentationType.SWAGGER_2)
  21. .apiInfo(apiInfo())
  22. //是否启动swagger,默认是true,可以不写,false就不能在浏览器中访问了
  23. .enable(true)
  24. .select()
  25. //RequestHandlerSelectors:配置要扫描接口的方式
  26. //basePackage:指定要扫描的包
  27. //any():扫描全部
  28. //none:不扫描
  29. //withClassAnnotaion:扫描类上的注解,参数是一个注解的反射对象
  30. //withMethodAnnotaion:扫描方法上的注解
  31. .apis(RequestHandlerSelectors.basePackage("com.lyd.swagger.controller"))
  32. //pathcs(),过滤什么路径
  33. .paths(PathSelectors.none())
  34. .build();
  35. }
  36. //配置swagger自定义信息
  37. private ApiInfo apiInfo(){
  38. return new ApiInfo(
  39. "API文档",
  40. "这是测试学习",
  41. "1.0",
  42. "urn:tos",
  43. new Contact("lyd","lyd.com","206977438@qq.com"),
  44. "Apache 2.0",
  45. "http://www.apache.org/licenses/LICENSE-2.0",
  46. new ArrayList());
  47. }
  48. }

配置Swagger运行环境

假设有这三种环境
image.png
设置swagger的运行环境

  1. package com.lyd.swagger.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.core.env.Environment;
  5. import org.springframework.core.env.Profiles;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import springfox.documentation.builders.PathSelectors;
  8. import springfox.documentation.builders.RequestHandlerSelectors;
  9. import springfox.documentation.service.ApiInfo;
  10. import springfox.documentation.service.Contact;
  11. import springfox.documentation.spi.DocumentationType;
  12. import springfox.documentation.spring.web.plugins.Docket;
  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  14. import java.util.ArrayList;
  15. @Configuration
  16. @EnableSwagger2 //启动swagger2
  17. public class SwaggerConfig {
  18. //配置了Swagger的Docket的bean实例
  19. @Bean
  20. public Docket docket(Environment environment){
  21. //设置要显示的swagger环境,可以设置多个
  22. Profiles profiles = Profiles.of("dev","test");
  23. //通过environment.acceptsProfiles判断是否处在自己设定的环境正当中
  24. boolean flag = environment.acceptsProfiles(profiles);
  25. //看源码,Docket里面很多属性
  26. return new Docket(DocumentationType.SWAGGER_2)
  27. .apiInfo(apiInfo())
  28. .enable(flag)//运行环境在设定的swagger显示环境中,就能运行Swagger
  29. .select()
  30. .apis(RequestHandlerSelectors.basePackage("com.lyd.swagger.controller"))
  31. .build();
  32. }
  33. //配置swagger自定义信息
  34. private ApiInfo apiInfo(){
  35. return new ApiInfo(
  36. "API文档",
  37. "这是测试学习",
  38. "1.0",
  39. "urn:tos",
  40. new Contact("lyd","lyd.com","206977438@qq.com"),
  41. "Apache 2.0",
  42. "http://www.apache.org/licenses/LICENSE-2.0",
  43. new ArrayList());
  44. }
  45. }

运行项目,如果设置的运行环境不是dev或者test,那么访问swagger页面就会出现这个
image.png
如果运行环境是dev,那么就能正确的访问swagger页面,注意不同运行环境的端口不要写错了

配置Swagger分组

  1. package com.lyd.swagger.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.core.env.Environment;
  5. import org.springframework.core.env.Profiles;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import springfox.documentation.builders.PathSelectors;
  8. import springfox.documentation.builders.RequestHandlerSelectors;
  9. import springfox.documentation.service.ApiInfo;
  10. import springfox.documentation.service.Contact;
  11. import springfox.documentation.spi.DocumentationType;
  12. import springfox.documentation.spring.web.plugins.Docket;
  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  14. import java.util.ArrayList;
  15. @Configuration
  16. @EnableSwagger2 //启动swagger2
  17. public class SwaggerConfig {
  18. //配置了Swagger的Docket的bean实例
  19. @Bean
  20. public Docket docket(Environment environment){
  21. //设置要显示的swagger环境,可以设置多个
  22. Profiles profiles = Profiles.of("dev","test");
  23. //通过environment.acceptsProfiles判断是否处在自己设定的环境正当中
  24. boolean flag = environment.acceptsProfiles(profiles);
  25. //看源码,Docket里面很多属性
  26. return new Docket(DocumentationType.SWAGGER_2)
  27. .apiInfo(apiInfo())
  28. .groupName("第一组")
  29. .enable(flag)//运行环境在设定的swagger显示环境中,就能运行Swagger
  30. .select()
  31. .apis(RequestHandlerSelectors.basePackage("com.lyd.swagger.controller"))
  32. .build();
  33. }
  34. //一个Docket bean一组,协同开发时,每个人一个Docket,或者每一个模块一个Docket分组
  35. @Bean
  36. public Docket docket2(){
  37. return new Docket(DocumentationType.SWAGGER_2).groupName("第二组");
  38. }
  39. //配置swagger自定义信息
  40. private ApiInfo apiInfo(){
  41. return new ApiInfo(
  42. "API文档",
  43. "这是测试学习",
  44. "1.0",
  45. "urn:tos",
  46. new Contact("lyd","lyd.com","206977438@qq.com"),
  47. "Apache 2.0",
  48. "http://www.apache.org/licenses/LICENSE-2.0",
  49. new ArrayList());
  50. }
  51. }

然后打开Swagger页面就可以看到,每一组显示的东西不一样
image.png

配置Swagger实体类以及注释

  1. package com.lyd.swagger.controller;
  2. import com.lyd.swagger.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. @RestController
  11. public class HelloController {
  12. //ApiOperation给接口操作加上注释
  13. @ApiOperation("Hello控制类")
  14. @GetMapping("/hello")
  15. public String hello(){
  16. return "这是hello路径的返回";
  17. }
  18. //只要我们接口中,返回值中存在实体类,他就会被扫描到Swagger中
  19. @PostMapping("/user")
  20. public User user(){
  21. return new User();
  22. }
  23. @ApiOperation("添加用户")
  24. @PostMapping("/adduser")
  25. //ApiParam给参数加上注释
  26. public String adduser(@ApiParam("用户名") String name){
  27. return "添加用户";
  28. }
  29. }
  1. package com.lyd.swagger.pojo;
  2. import io.swagger.annotations.Api;
  3. import io.swagger.annotations.ApiModel;
  4. import io.swagger.annotations.ApiModelProperty;
  5. //给实体类加上注释
  6. @ApiModel("用户实体类")
  7. public class User {
  8. //给实体类参数加上注释
  9. @ApiModelProperty("姓名")
  10. private String name;
  11. @ApiModelProperty("年龄")
  12. private int age;
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public int getAge() {
  20. return age;
  21. }
  22. public void setAge(int age) {
  23. this.age = age;
  24. }
  25. }