在springboot项目中如何配置Swagger Api在线文档,同时给文档访问添加权限控制。

一、这里使用的是bootstrap版的API文档

二、如何在项目中配置,步骤如下

1、在pom文件中添加jar依赖

 
  1. <!-- Swagger -->

  2. <dependency>

  3. <groupId>io.springfox</groupId>

  4. <artifactId>springfox-swagger2</artifactId>

  5. <version>2.9.2</version>

  6. </dependency>

  7.  
  8. <!-- 根据自己需求选择不同版本的UI -->

  9.  
  10. <!-- Swagger 官方UI jar包 -->

  11. <!--<dependency>-->

  12. <!--<groupId>io.springfox</groupId>-->

  13. <!--<artifactId>springfox-swagger-ui</artifactId>-->

  14. <!--<version>2.9.2</version>-->

  15. <!--</dependency>-->

  16.  
  17. <!-- 萧明同学提供的 jar 包 -->

  18. <dependency>

  19. <groupId>com.github.xiaoymin</groupId>

  20. <artifactId>swagger-bootstrap-ui</artifactId>

  21. <version>1.9.3</version>

  22. </dependency>

2、在application.yml文件中添加以下权限访问口令

 
  1. spring:

  2. profiles:

  3. active: test

  4. # Swagger Tag 排序

  5. swagger:

  6. ui-config:

  7. # method<按方法定义顺序排序>

  8. operations-sorter: method

  9. basic:

  10. enable: true

  11. ## Basic认证用户名

  12. username: 自定义

  13. ## Basic认证密码

  14. password: 自定义

3、在项目中创建swagger配置类

 
  1. import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;

  2. import org.springframework.context.annotation.Bean;

  3. import org.springframework.context.annotation.Configuration;

  4. import springfox.documentation.builders.ApiInfoBuilder;

  5. import springfox.documentation.builders.ParameterBuilder;

  6. import springfox.documentation.builders.PathSelectors;

  7. import springfox.documentation.builders.RequestHandlerSelectors;

  8. import springfox.documentation.schema.ModelRef;

  9. import springfox.documentation.service.ApiInfo;

  10. import springfox.documentation.service.Parameter;

  11. import springfox.documentation.spi.DocumentationType;

  12. import springfox.documentation.spring.web.plugins.Docket;

  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;

  14.  
  15. import java.util.ArrayList;

  16. import java.util.List;

  17.  
  18. /**

  19. * @ClassName Swagger2Config

  20. * @Description: TODO

  21. * @Author wzwsq

  22. * @Date 2020/6/29

  23. * @Version V1.0

  24. **/

  25. @Configuration

  26. @EnableSwagger2

  27. @EnableSwaggerBootstrapUI

  28. public class Swagger2Config {

  29. /**

  30. * 全局参数

  31. *

  32. * @return List<Parameter>

  33. */

  34. private List<Parameter> parameter() {

  35. List<Parameter> params = new ArrayList<>();

  36. params.add(new ParameterBuilder().name("token")

  37. .description("认证令牌")

  38. .modelRef(new ModelRef("string"))

  39. .parameterType("header")

  40. .required(false).build());

  41. return params;

  42. }

  43.  
  44.  
  45. //swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等

  46. @Bean

  47. public Docket createRestApi() {

  48. return new Docket(DocumentationType.SWAGGER_2)

  49. .apiInfo(apiInfo())

  50. .select()

  51. //为当前包路径

  52. .apis(RequestHandlerSelectors.basePackage("xxx.controller"))

  53. .paths(PathSelectors.any())

  54. .build().globalOperationParameters(parameter());

  55. }

  56. //构建 api文档的详细信息函数,注意这里的注解引用的是哪个

  57. private ApiInfo apiInfo() {

  58. return new ApiInfoBuilder()

  59. //页面标题

  60. .title("MY API")

  61. //创建人

  62. //版本号

  63. .version("1.0")

  64. //描述

  65. .description("API 描述")

  66. .build();

  67. }

  68. }

4、对于Controller类的API描述

 
  1. @Api(tags = { "UsersController" }, description = "用户操作常用接口")

  2. @RestController

  3. @RequestMapping("/users/")

  4. public class UsersController {

  5. private static Logger logger = LoggerFactory.getLogger(UsersController.class);

  6. @Autowired

  7. private UsersService usersService;

  8.  
  9.  
  10. @ApiOperation("根据用户id,查询用户信息")

  11. @RequestMapping(value = "toDetail", method = RequestMethod.GET)

  12. public JSONObject editUsers(@RequestParam("id")String id){

  13. logger.info("------------toDetail ---------------");

  14. JSONObject jsonObject = new JSONObject();

  15. //根据用户id,获取用户信息

  16. UsersModel usersModel=usersService.getUserById(id);

  17. jsonObject.put("users",usersModel);

  18. return jsonObject;

  19. }

  20. }

6、对于model类的API描述

 
  1. @ApiModel(description = "用户请求模型")

  2. public class UserRequest {

  3. /**

  4. * 用户名

  5. */

  6. @ApiModelProperty("用户名")

  7. private Integer userName;

  8.  
  9. /**

  10. * 用户密码

  11. */

  12. @ApiModelProperty("用户密码")

  13. private Integer userPass;

  14.  
  15. }

 5、启动springboot

无项目名访问地址如下

http://localhost:8080/doc.html

有项目名访问地址如下

http://localhost:8080/my/doc.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值