maven配置:

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

gradle配置:

  1. implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
  2. implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'

SwaggerConfig配置:

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import springfox.documentation.builders.ApiInfoBuilder;
  4. import springfox.documentation.builders.PathSelectors;
  5. import springfox.documentation.builders.RequestHandlerSelectors;
  6. import springfox.documentation.service.ApiInfo;
  7. import springfox.documentation.spi.DocumentationType;
  8. import springfox.documentation.spring.web.plugins.Docket;
  9. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  10. /**
  11. 1. swagger配置类
  12. */
  13. @Configuration
  14. @EnableSwagger2
  15. public class SwaggerConfig {
  16. @Bean
  17. public Docket createRestApi() {
  18. return new Docket(DocumentationType.SWAGGER_2)
  19. .apiInfo(apiInfo())
  20. //是否开启 (true 开启 false隐藏。生产环境建议隐藏)
  21. //.enable(false)
  22. .select()
  23. //扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
  24. .apis(RequestHandlerSelectors.basePackage("com.yaol.bootdemo.controller"))
  25. //指定路径处理PathSelectors.any()代表所有的路径
  26. .paths(PathSelectors.any())
  27. .build();
  28. }
  29. private ApiInfo apiInfo() {
  30. return new ApiInfoBuilder()
  31. //设置文档标题(API名称)
  32. .title("SpringBoot中使用Swagger2接口规范")
  33. //文档描述
  34. .description("接口说明")
  35. //服务条款URL
  36. .termsOfServiceUrl("http://localhost:8080/")
  37. //版本号
  38. .version("1.0.0")
  39. .build();
  40. }
  41. }

http访问:

http://localhost:8080/swagger-ui.html
🚀 swagger使用 - 图1

controller示例:

  1. import com.yaol.bootdemo.entity.User;
  2. import com.yaol.bootdemo.service.UserService;
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiImplicitParam;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.DeleteMapping;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. //说明接口文件
  12. @Api(value = "测试接口", tags = "用户管理相关的接口", description = "用户测试接口")
  13. @RestController
  14. public class UserController {
  15. @Autowired
  16. private UserService userService;
  17. //方法参数说明,name参数名;value参数说明,备注;dataType参数类型;required 是否必传;defaultValue 默认值
  18. @ApiImplicitParam(name = "user", value = "新增用户数据")
  19. //说明是什么方法(可以理解为方法注释)
  20. @ApiOperation(value = "添加用户", notes = "添加用户")
  21. @RequestMapping("/save")
  22. public String save(User user){
  23. userService.save(user);
  24. return "ok";
  25. }
  26. /**
  27. * 根据id查询用户
  28. * @param id
  29. * @return
  30. */
  31. @GetMapping(value = "findById")
  32. @ApiOperation(value = "根据id获取用户信息", notes = "根据id查询用户信息")
  33. public User getUser(Integer id){
  34. return userService.getById(id);
  35. }
  36. @DeleteMapping(value = "deleteById")
  37. @ApiOperation(value = "根据id删除数据", notes = "删除用户")
  38. public String delete(Integer id){
  39. userService.removeById(id);
  40. return "删除成功";
  41. }
  42. }

模型vo示例:

  1. import com.baomidou.mybatisplus.annotation.TableName;
  2. import io.swagger.annotations.ApiModel;
  3. import io.swagger.annotations.ApiModelProperty;
  4. import lombok.Data;
  5. @ApiModel(description= "返回响应数据")
  6. @Data
  7. @TableName(value = "user")
  8. public class User {
  9. @ApiModelProperty("用户id")
  10. private Long id;
  11. @ApiModelProperty("用户姓名")
  12. private String name;
  13. }