Springdoc替換swagger的實(shí)現(xiàn)步驟分解
前言
距離swagger上次發(fā)布版本已經(jīng)過去兩年多了,一直沒有更新,與當(dāng)前的springboot2.6.x、springboot2.7.x存在各種兼容問題,對于即將發(fā)布的springboot3.x,可能存在更多兼容問題。如下圖所示。
其實(shí)swagger還在更新,應(yīng)該是springfox不更新導(dǎo)致的,所以需要使用其他的API管理工具代替,springdoc是一種選擇
一、springdoc介紹
SpringDoc是一款可以結(jié)合SpringBoot使用的API文檔生成工具,基于OpenAPI 3,是一款更好用的Swagger庫!值得一提的是SpringDoc不僅支持Spring WebMvc項(xiàng)目,還可以支持Spring WebFlux項(xiàng)目,甚至Spring Rest和Spring Native項(xiàng)目。
二、使用步驟
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配置類
創(chuàng)建一個(gè)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("權(quán)限相關(guān)") .packagesToScan("com.iscas.biz.controller.common.auth") .build(); } @Bean public GroupedOpenApi adminApi() { return GroupedOpenApi.builder() .group("默認(rèn)") .pathsToMatch("/**") .build(); } }
3.常用的swagger注解和springdoc的對應(yīng)關(guān)系
4.一個(gè)接口類的示例
@Tag(name = "組織機(jī)構(gòu)管理-OrgController") @RestController @RequestMapping("/org") @Validated @ConditionalOnMybatis public class OrgController extends BaseController { private final OrgService orgService; public OrgController(OrgService orgService) { this.orgService = orgService; } @Operation(summary="[組織機(jī)構(gòu)]獲取組織機(jī)構(gòu)樹", description="create by:朱全文 2021-02-20") @GetMapping public TreeResponse get() throws BaseException { return getTreeResponse().setValue(orgService.getTree()); } @Operation(summary="[組織機(jī)構(gòu)]新增組織機(jī)構(gòu)節(jié)點(diǎn)", description="create by:朱全文 2021-02-20") @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "組織機(jī)構(gòu)數(shù)據(jù)", 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="[組織機(jī)構(gòu)]修改組織機(jī)構(gòu)節(jié)點(diǎn)", description="create by:朱全文 2021-02-20") @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "組織機(jī)構(gòu)數(shù)據(jù)", content = @Content(schema = @Schema(implementation = Org.class))) @PutMapping("/node") public ResponseEntity editNode(@Valid @RequestBody Org org) { return getResponse().setValue(orgService.editOrg(org)); } @Operation(summary="[組織機(jī)構(gòu)]刪除組織機(jī)構(gòu)節(jié)點(diǎn)", description="create by:朱全文 2021-02-20") @io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "組織機(jī)構(gòu)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
還有其他的各種配置,可以在寫配置的時(shí)候查看提示
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,如下圖所示:
到此這篇關(guān)于Springdoc替換swagger的實(shí)現(xiàn)步驟分解的文章就介紹到這了,更多相關(guān)Springdoc替換swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 中普通代碼塊,構(gòu)造代碼塊,靜態(tài)代碼塊區(qū)別及代碼示例
這篇文章主要介紹了Java 中普通代碼塊,構(gòu)造代碼塊,靜態(tài)代碼塊區(qū)別及代碼示例的相關(guān)資料,需要的朋友可以參考下2017-01-01解決IDEA中Maven依賴包導(dǎo)入失敗報(bào)紅問題(總結(jié)最有效8種解決方案)
這篇文章主要介紹了解決IDEA中Maven依賴包導(dǎo)入失敗報(bào)紅問題,本文通過圖文詳解給大家總結(jié)了最有效的8種解決方法,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Java實(shí)現(xiàn)可視化走迷宮小游戲的示例代碼
這篇文章主要介紹了Java如何實(shí)現(xiàn)可視化走迷宮小游戲。本程序適用于java程序員鞏固類與對象、文件讀取、事件響應(yīng)、awt包中各種工具的相關(guān)概念以及對邏輯能力的鍛煉,需要的可以參考一下2022-11-11