这篇文章主要介绍“Springdoc替换swagger怎么实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Springdoc替换swagger怎么实现”文章能帮助大家解决问题。
前言
距离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:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.11</version>
</dependency>
2.spring配置类
创建一个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();
}
}
3.常用的swagger注解和springdoc的对应关系
4.一个接口类的示例
@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();
}
}
5.配置文件配置
springdoc.swagger-ui.doc-expansion=none
springdoc.swagger-ui.path=/doc.html
还有其他的各种配置,可以在写配置的时候查看提示
6.WebMvc配置
@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");
}
}
7.UI
访问地址:http://localhost:7901/demo/swagger-ui/ 或 http://localhost:7901/demo/doc.html
UI还使用swagger的UI,如下图所示: