距离swagger上次发布版本已经过去两年多了,一直没有更新,与当前的springboot2.6.x、springboot2.7.x存在各种兼容问题,对于即将发布的springboot3.x,可能存在更多兼容问题。如下图所示。
其实swagger还在更新,应该是springfox不更新导致的,所以需要使用其他的API管理工具代替,springdoc是一种选择
SpringDoc是一款可以结合SpringBoot使用的API文档生成工具,基于OpenAPI 3,是一款更好用的Swagger库!值得一提的是SpringDoc不仅支持Spring WebMvc项目,还可以支持Spring WebFlux项目,甚至Spring Rest和Spring Native项目。
gradle:
api group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.11'
maven:
<dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.6.11</version> </dependency>
创建一个spring配置类,添加springdoc的配置
@AutoConfiguration public class SpringDocConfig { @Bean public OpenAPI openAPI() { return new OpenAPI() .info(new Info() .title("newframe-接口文档") .description("基于SpringDoc的在线接口文档") .version("0.0.1")); } @Bean public GroupedOpenApi publicApi() { return GroupedOpenApi.builder() .group("权限相关") .packagesToScan("com.iscas.biz.controller.common.auth") .build(); } @Bean public GroupedOpenApi adminApi() { return GroupedOpenApi.builder() .group("默认") .pathsToMatch("/**") .build(); } }
@Tag(name = "组织机构管理-OrgController") @RestController @RequestMapping("/org") @Validated @ConditionalOnMybatis public class OrgController extends BaseController { private final OrgService orgService; public OrgController(OrgService orgService) { this.orgService = orgService; } @Operation(summary="[组织机构]获取组织机构树", description="create by:朱全文 2021-02-20") @GetMapping public TreeResponse get() throws BaseException { return getTreeResponse().setValue(orgService.getTree()); } @Operation(summary="[组织机构]新增组织机构节点", description="create by:朱全文 2021-02-20") @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "组织机构数据", content = @Content(schema = @Schema(implementation = Org.class))) @PostMapping("/node") public ResponseEntity addNode(@Valid @RequestBody Org org) throws BaseException { return getResponse().setValue(orgService.addOrg(org)); } @Operation(summary="[组织机构]修改组织机构节点", description="create by:朱全文 2021-02-20") @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "组织机构数据", content = @Content(schema = @Schema(implementation = Org.class))) @PutMapping("/node") public ResponseEntity editNode(@Valid @RequestBody Org org) { return getResponse().setValue(orgService.editOrg(org)); } @Operation(summary="[组织机构]删除组织机构节点", description="create by:朱全文 2021-02-20") @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "组织机构ID集合", content = @Content(examples = @ExampleObject(value = "[123, 124]"))) @PostMapping("/node/del") @Caching(evict = { @CacheEvict(value = "auth", key = "'url_map'"), @CacheEvict(value = "auth", key = "'menus'"), @CacheEvict(value = "auth", key = "'role_map'") }) public ResponseEntity deleteNode(@RequestBody List<Integer> orgIds) { AssertCollectionUtils.assertCollectionNotEmpty(orgIds, "orgIds不能未空"); orgService.deleteNode(orgIds); return getResponse(); } }
springdoc.swagger-ui.doc-expansion=none
springdoc.swagger-ui.path=/doc.html
还有其他的各种配置,可以在写配置的时候查看提示
@AutoConfiguration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry. addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") .resourceChain(false); registry. addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/swagger-ui/") .setViewName("forward:/swagger-ui/index.html"); } }
访问地址:http://localhost:7901/demo/swagger-ui/ 或 http://localhost:7901/demo/doc.html
UI还使用swagger的UI,如下图所示:
到此这篇关于Springdoc替换swagger的实现步骤分解的文章就介绍到这了,更多相关Springdoc替换swagger内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
编程 | 2023-02-24 21:36
编程 | 2023-02-21 12:51
编程 | 2023-02-21 12:47
编程 | 2023-02-21 00:15
编程 | 2023-02-21 00:08
编程 | 2023-02-20 21:46
编程 | 2023-02-20 21:42
编程 | 2023-02-20 21:36
编程 | 2023-02-20 21:32
编程 | 2023-02-20 18:12
网友评论