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,如下圖所示:

到此這篇關於Springdoc替換swagger的實現步驟分解的文章就介紹到這瞭,更多相關Springdoc替換swagger內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: