经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
Springdoc替换swagger的实现步骤分解
来源:jb51  时间:2023/2/17 9:59:05  对本文有异议

前言

距离swagger上次发布版本已经过去两年多了,一直没有更新,与当前的springboot2.6.x、springboot2.7.x存在各种兼容问题,对于即将发布的springboot3.x,可能存在更多兼容问题。如下图所示。

其实swagger还在更新,应该是springfox不更新导致的,所以需要使用其他的API管理工具代替,springdoc是一种选择

一、springdoc介绍

SpringDoc是一款可以结合SpringBoot使用的API文档生成工具,基于OpenAPI 3,是一款更好用的Swagger库!值得一提的是SpringDoc不仅支持Spring WebMvc项目,还可以支持Spring WebFlux项目,甚至Spring Rest和Spring Native项目。

二、使用步骤

1.引入库

gradle:

api group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.11'

maven:

  1. <dependency>
  2. <groupId>org.springdoc</groupId>
  3. <artifactId>springdoc-openapi-ui</artifactId>
  4. <version>1.6.11</version>
  5. </dependency>

2.spring配置类

创建一个spring配置类,添加springdoc的配置

  1. @AutoConfiguration
  2. public class SpringDocConfig {
  3. @Bean
  4. public OpenAPI openAPI() {
  5. return new OpenAPI()
  6. .info(new Info()
  7. .title("newframe-接口文档")
  8. .description("基于SpringDoc的在线接口文档")
  9. .version("0.0.1"));
  10. }
  11. @Bean
  12. public GroupedOpenApi publicApi() {
  13. return GroupedOpenApi.builder()
  14. .group("权限相关")
  15. .packagesToScan("com.iscas.biz.controller.common.auth")
  16. .build();
  17. }
  18. @Bean
  19. public GroupedOpenApi adminApi() {
  20. return GroupedOpenApi.builder()
  21. .group("默认")
  22. .pathsToMatch("/**")
  23. .build();
  24. }
  25. }

3.常用的swagger注解和springdoc的对应关系

4.一个接口类的示例

  1. @Tag(name = "组织机构管理-OrgController")
  2. @RestController
  3. @RequestMapping("/org")
  4. @Validated
  5. @ConditionalOnMybatis
  6. public class OrgController extends BaseController {
  7. private final OrgService orgService;
  8.  
  9. public OrgController(OrgService orgService) {
  10. this.orgService = orgService;
  11. }
  12. @Operation(summary="[组织机构]获取组织机构树", description="create by:朱全文 2021-02-20")
  13. @GetMapping
  14. public TreeResponse get() throws BaseException {
  15. return getTreeResponse().setValue(orgService.getTree());
  16. }
  17. @Operation(summary="[组织机构]新增组织机构节点", description="create by:朱全文 2021-02-20")
  18. @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "组织机构数据",
  19. content = @Content(schema = @Schema(implementation = Org.class)))
  20. @PostMapping("/node")
  21. public ResponseEntity addNode(@Valid @RequestBody Org org) throws BaseException {
  22. return getResponse().setValue(orgService.addOrg(org));
  23. }
  24. @Operation(summary="[组织机构]修改组织机构节点", description="create by:朱全文 2021-02-20")
  25. @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "组织机构数据",
  26. content = @Content(schema = @Schema(implementation = Org.class)))
  27. @PutMapping("/node")
  28. public ResponseEntity editNode(@Valid @RequestBody Org org) {
  29. return getResponse().setValue(orgService.editOrg(org));
  30. }
  31. @Operation(summary="[组织机构]删除组织机构节点", description="create by:朱全文 2021-02-20")
  32. @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "组织机构ID集合", content = @Content(examples = @ExampleObject(value = "[123, 124]")))
  33. @PostMapping("/node/del")
  34. @Caching(evict = {
  35. @CacheEvict(value = "auth", key = "'url_map'"),
  36. @CacheEvict(value = "auth", key = "'menus'"),
  37. @CacheEvict(value = "auth", key = "'role_map'")
  38. })
  39. public ResponseEntity deleteNode(@RequestBody List<Integer> orgIds) {
  40. AssertCollectionUtils.assertCollectionNotEmpty(orgIds, "orgIds不能未空");
  41. orgService.deleteNode(orgIds);
  42. return getResponse();
  43. }
  44. }

5.配置文件配置

springdoc.swagger-ui.doc-expansion=none
springdoc.swagger-ui.path=/doc.html

还有其他的各种配置,可以在写配置的时候查看提示

6.WebMvc配置

  1. @AutoConfiguration
  2. public class WebMvcConfig implements WebMvcConfigurer {
  3. @Override
  4. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  5. registry.
  6. addResourceHandler("/swagger-ui/**")
  7. .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
  8. .resourceChain(false);
  9. registry.
  10. addResourceHandler("/webjars/**")
  11. .addResourceLocations("classpath:/META-INF/resources/webjars/");
  12. }
  13. @Override
  14. public void addViewControllers(ViewControllerRegistry registry) {
  15. registry.addViewController("/swagger-ui/")
  16. .setViewName("forward:/swagger-ui/index.html");
  17. }
  18. }

7.UI

访问地址:http://localhost:7901/demo/swagger-ui/ 或 http://localhost:7901/demo/doc.html
UI还使用swagger的UI,如下图所示:

到此这篇关于Springdoc替换swagger的实现步骤分解的文章就介绍到这了,更多相关Springdoc替换swagger内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号