当前位置:   article > 正文

SpringBoot3 集成Springdoc 实现Swagger3功能_springdoc-openapi-starter-webmvc-ui

springdoc-openapi-starter-webmvc-ui

说明: 只通过引用org.springdoc 的两个包就可以使用Swagger3 功能(步骤1);如想更美观及实现动态认证的开启与关闭,及Swagger3登录认证等功能,需实现(步骤1、2、3)的配置; 

1、 引包

  1. <dependency>
  2. <groupId>org.springdoc</groupId>
  3. <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  4. <version>2.2.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springdoc</groupId>
  8. <artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
  9. <version>2.2.0</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.github.xiaoymin</groupId>
  13. <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
  14. <version>4.4.0</version>
  15. </dependency>

2、配置

  1. import io.swagger.v3.oas.models.security.SecurityRequirement;
  2. import io.swagger.v3.oas.models.security.SecurityScheme;
  3. import io.swagger.v3.oas.models.Components;
  4. import io.swagger.v3.oas.models.info.Info;
  5. import io.swagger.v3.oas.models.OpenAPI;
  6. import io.swagger.v3.oas.models.info.Contact;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. /**
  13. * Springboot3 + springdoc 实现swagger
  14. * 原生地址 http://www.xx.xx/swagger-ui/index.html
  15. * 美化地址 http://www.xx.xx/doc.html
  16. */
  17. @Configuration
  18. public class Swagger {
  19. @Value("${swagger.token}")
  20. private String token = "X_TOKEN";
  21. @Value("${swagger.enable-token}")
  22. private boolean enableToken = true;
  23. @Bean
  24. public OpenAPI springShopOpenAPI() {
  25. OpenAPI openApi = new OpenAPI()
  26. .info(new Info().title("测试文档")
  27. .contact(new Contact().name("zc"))
  28. .description("我的API文档")
  29. .version("v1"));
  30. if(enableToken){
  31. openApi.components(components()).security(securityRequirement());
  32. }
  33. return openApi;
  34. }
  35. /**
  36. * 设置授权认证信息
  37. * @return
  38. */
  39. private Components components(){
  40. return new Components()
  41. // 设置 spring security jwt accessToken 认证的请求头 Authorization: Bearer xxx.xxx.xxx
  42. .addSecuritySchemes(token, new SecurityScheme()
  43. .type(SecurityScheme.Type.HTTP)
  44. .bearerFormat("JWT")
  45. .in(SecurityScheme.In.HEADER)
  46. .name("Authorization")
  47. .scheme("Bearer"));
  48. }
  49. private List<SecurityRequirement> securityRequirement(){
  50. List<SecurityRequirement> securityRequirementList = new ArrayList<>();
  51. securityRequirementList.add(new SecurityRequirement().addList(token));
  52. return securityRequirementList;
  53. }
  54. }

3、 配置加强文件

  1. swagger:
  2. # 是否开启token 认证
  3. enable-token: false
  4. # token认证 key
  5. token: X_TOKEN
  6. springdoc:
  7. swagger-ui:
  8. path: /swagger-ui.html
  9. tags-sorter: alpha
  10. operations-sorter: alpha
  11. api-docs:
  12. path: /v3/api-docs
  13. group-configs:
  14. - group: 'default'
  15. paths-to-match: '/**'
  16. # 生成文档所需的扫包路径
  17. packages-to-scan: com.zc.controller
  18. ##knife4j 增强配置
  19. knife4j:
  20. #是否启用增强设置
  21. enable: true
  22. #开启生产环境屏蔽
  23. production: false
  24. #是否启用登录认证
  25. basic:
  26. enable: true
  27. username: admin
  28. password: 123456
  29. setting: # 前端UI的个性化配置属性
  30. language: zh_cn # 显示语言中文
  31. enable-version: true
  32. enable-swagger-models: true # 是否显示界面中SwaggerModel功能
  33. swagger-model-name: SwaggerModel2 # 重命名SwaggerModel名称,默认
  34. enable-document-manage: true # 是否显示界面中"文档管理"功能

4、controller

这里有个问题:controller 里面不加security = @SecurityRequirement(name = "X_TOKEN") 美化的doc.html路径的token 会失效;swagger-ui/index.html  路径的正常; X_TOKEN 就是配置文件中的token值;  如有好的处理办法,请各位大神指教一下;

  1. import com.zc.bean.HostDiffBean;
  2. import com.zc.service.TestHostService;
  3. import io.swagger.v3.oas.annotations.Operation;
  4. import io.swagger.v3.oas.annotations.Parameter;
  5. import io.swagger.v3.oas.annotations.security.SecurityRequirement;
  6. import io.swagger.v3.oas.annotations.tags.Tag;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.web.bind.annotation.GetMapping;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import java.util.List;
  14. /**
  15. * @author zc
  16. * @date 2024/2/23 13:29
  17. * @desc
  18. */
  19. @Tag(name = "Test")
  20. @RestController
  21. @RequestMapping("/test1")
  22. public class TestSwagger3Controller {
  23. @Value("${swagger.token}")
  24. private String token = "X_TOKEN";
  25. @Autowired
  26. private TestHostService testHostService;
  27. @GetMapping("/get")
  28. @Operation(summary = "测试方法", description = "测试方法", security = @SecurityRequirement(name = "X_TOKEN"))
  29. public List<HostDiffBean> test(@RequestParam(name = "pageSize") @Parameter(name = "pageSize", description = "页数") Integer pageSize){
  30. return testHostService.list();
  31. }
  32. @GetMapping("/getHost")
  33. @Operation(summary = "测试方法", description = "测试方法", security = @SecurityRequirement(name = "X_TOKEN"))
  34. public List<HostDiffBean> testHost(@RequestParam(name = "pageSize") @Parameter(name = "pageSize", description = "页数") Integer pageSize){
  35. return testHostService.list();
  36. }
  37. }

5、bean

  1. import com.baomidou.mybatisplus.annotation.IdType;
  2. import com.baomidou.mybatisplus.annotation.TableField;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import com.baomidou.mybatisplus.annotation.TableName;
  5. import com.zc.enums.SexEnum;
  6. import io.swagger.v3.oas.annotations.media.Schema;
  7. import lombok.Data;
  8. import java.io.Serializable;
  9. import java.util.Date;
  10. /**
  11. * @author zc
  12. * @date 2024/3/14 17:16
  13. * @desc
  14. */
  15. @Data
  16. @TableName("cm_host_compare_diff")
  17. @Schema(description = "测试表")
  18. public class HostDiffBean implements Serializable {
  19. /**
  20. * id
  21. */
  22. @TableId(type = IdType.ASSIGN_UUID)
  23. @Schema(description = "主键")
  24. private String id;
  25. /**
  26. * 序列号
  27. */
  28. @Schema(description = "序列号")
  29. private String serialNumber;
  30. /**
  31. * ip
  32. */
  33. private String ip;
  34. /**
  35. * 设备型号
  36. */
  37. private String deviceNo;
  38. /**
  39. * 华为端ip
  40. */
  41. @TableField(value = "h_ip")
  42. private String hIp;
  43. /**
  44. * 华为端设备型号
  45. */
  46. @TableField(value = "h_device_No")
  47. private String hDeviceNo;
  48. /**
  49. * 创建时间
  50. */
  51. private Date createTime;
  52. /**
  53. * 性别
  54. */
  55. private SexEnum sex;
  56. }

6、参考

springboot集成springdoc-openapi(模拟前端请求)_springdoc-openapi-ui-CSDN博客

Java21 + SpringBoot3整合springdoc-openapi,自动生成在线接口文档,支持SpringSecurity和JWT认证方式_java 使用springdoc-openapi-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/899428
推荐阅读
相关标签
  

闽ICP备14008679号