SpringBoot整合Swagger Api自動生成文檔的實(shí)現(xiàn)
Swagger 是一套 RESTful API 文檔生成工具,可以方便地生成 API 文檔并提供 API 調(diào)試頁面。而 Spring Boot 是一款非常優(yōu)秀的 Java Web 開發(fā)框架,它可以非常方便地構(gòu)建 Web 應(yīng)用程序。在本文中,我們將介紹如何使用 Swagger 以及如何在 Spring Boot 中整合 Swagger。
一、添加 Swagger 依賴
首先,在 pom.xml 文件中添加 Swagger 的依賴:
<!-- swagger2 --> <dependency> ? ? <groupId>io.springfox</groupId> ? ? <artifactId>springfox-swagger2</artifactId> ? ? <version>2.9.2</version> </dependency> <!-- swagger-ui --> <dependency> ? ? <groupId>io.springfox</groupId> ? ? <artifactId>springfox-swagger-ui</artifactId> ? ? <version>2.9.2</version> </dependency>
二、創(chuàng)建接口類
在 Spring Boot 項(xiàng)目中創(chuàng)建一個 Controller,并在該 Controller 中添加一個簡單的接口。例如,我們創(chuàng)建一個名為 HelloController 的 Controller 類,并添加一個 /hello 接口,返回一個字符串 "hello world"。
@RestController public class HelloController { ? ? @GetMapping("/hello") ? ? public String hello() { ? ? ? ? return "hello world"; ? ? } }
三、添加 Swagger 配置類
接下來,我們需要創(chuàng)建一個 Swagger 配置類,用于配置 Swagger 文檔的生成方式。在項(xiàng)目中創(chuàng)建一個名為 SwaggerConfig 的類,添加如下代碼:
@Configuration @EnableSwagger2 public class SwaggerConfig { ? ? @Bean ? ? public Docket createRestApi() { ? ? ? ? return new Docket(DocumentationType.SWAGGER_2) ? ? ? ? ? ? ? ? .apiInfo(apiInfo()) ? ? ? ? ? ? ? ? .select() ? ? ? ? ? ? ? ? .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) ? ? ? ? ? ? ? ? .paths(PathSelectors.any()) ? ? ? ? ? ? ? ? .build(); ? ? } ? ? private ApiInfo apiInfo() { ? ? ? ? return new ApiInfoBuilder() ? ? ? ? ? ? ? ? .title("Spring Boot 中使用 Swagger2 構(gòu)建 RESTful APIs") ? ? ? ? ? ? ? ? .description("更多 Spring Boot 相關(guān)文章請關(guān)注:https://www.example.com/") ? ? ? ? ? ? ? ? .termsOfServiceUrl("https://www.example.com/") ? ? ? ? ? ? ? ? .contact(new Contact("binjie09", "", "binjie09@example.com")) ? ? ? ? ? ? ? ? .version("1.0") ? ? ? ? ? ? ? ? .build(); ? ? } }
其中,createRestApi() 方法用于創(chuàng)建一個 Docket 對象,該對象包含了 Swagger 文檔的生成方式設(shè)置。在上述代碼中,我們設(shè)置了文檔的基本信息,以及包掃描路徑等。
四、訪問 Swagger 頁面
最后,啟動 Spring Boot 應(yīng)用程序,訪問 http://localhost:8080/swagger-ui.html 即可看到 Swagger 文檔頁面。在頁面左側(cè),我們可以看到程序中創(chuàng)建的 API 接口,點(diǎn)擊接口后可以看到該接口的詳細(xì)信息。
在 Swagger 頁面上,我們還可以進(jìn)行接口測試和調(diào)試。在 /hello 接口上,點(diǎn)擊 "Try it out" 按鈕,填寫請求參數(shù)后點(diǎn)擊 "Execute" 按鈕,即可發(fā)送請求并獲取響應(yīng)。
通過整合 Swagger,我們可以在 Spring Boot 應(yīng)用程序中快速生成 RESTful API 文檔,并且直接在頁面上進(jìn)行調(diào)試和測試,非常方便。
五、整合一個更友好的UI接口文檔 Knife4j
Knife4j 是一款基于 Swagger 的 API 文檔生成工具,它提供了非常友好的 UI 界面,可以方便地生成和瀏覽 API 文檔。在本文中,我們將介紹如何在 Spring Boot 中整合 Knife4j。
1、添加 Knife4j 依賴
首先,在 pom.xml 文件中添加 Knife4j 的依賴:
<!-- knife4j --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi2-spring-boot-starter</artifactId> <version>4.1.0</version> </dependency>
2、添加 Knife4j 配置類
接下來,我們需要創(chuàng)建一個 Knife4j 配置類,用于配置 Knife4j 文檔的生成方式。在項(xiàng)目中創(chuàng)建一個名為 Knife4jConfig 的類,添加如下代碼:
@Configuration @EnableKnife4j public class Knife4jConfig { ? ? @Bean ? ? public Docket docket() { ? ? ? ? return new Docket(DocumentationType.SWAGGER_2) ? ? ? ? ? ? ? ? .apiInfo(apiInfo()) ? ? ? ? ? ? ? ? .select() ? ? ? ? ? ? ? ? .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) ? ? ? ? ? ? ? ? .paths(PathSelectors.any()) ? ? ? ? ? ? ? ? .build(); ? ? } ? ? private ApiInfo apiInfo() { ? ? ? ? return new ApiInfoBuilder() ? ? ? ? ? ? ? ? .title("Spring Boot 中使用 Knife4j 構(gòu)建 RESTful APIs") ? ? ? ? ? ? ? ? .description("更多 Spring Boot 相關(guān)文章請關(guān)注:https://www.example.com/") ? ? ? ? ? ? ? ? .termsOfServiceUrl("https://www.example.com/") ? ? ? ? ? ? ? ? .contact(new Contact("binjie09", "", "binjie09@example.com")) ? ? ? ? ? ? ? ? .version("1.0") ? ? ? ? ? ? ? ? .build(); ? ? } }
在上述代碼中,我們使用了 @EnableKnife4j 注解開啟了 Knife4j 的自動配置。在 docket() 方法中,我們設(shè)置了文檔的基本信息、包掃描路徑等。需要注意的是,在 Knife4j 中,我們需要使用 Swagger 2.x 版本的 API。
3、訪問 Knife4j 頁面
最后,啟動 Spring Boot 應(yīng)用程序,訪問 http://localhost:8080/doc.html 即可看到 Knife4j 文檔頁面。與 Swagger 相比,Knife4j 提供了更加友好的 UI 界面,并且可以直接在頁面上進(jìn)行接口測試和調(diào)試,非常方便。
通過整合 Knife4j,我們可以在 Spring Boot 應(yīng)用程序中快速生成 RESTful API 文檔,并且直接在頁面上進(jìn)行調(diào)試和測試,提高了開發(fā)效率。
到此這篇關(guān)于SpringBoot整合Swagger Api自動生成文檔的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Swagger Api自動生成 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決springboot 2.x 里面訪問靜態(tài)資源的坑
這篇文章主要介紹了解決springboot 2.x 里面訪問靜態(tài)資源的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08SpringMVC開發(fā)restful API之用戶查詢代碼詳解
這篇文章主要介紹了SpringMVC開發(fā)restful API之用戶查詢代碼詳解,小編覺得挺不錯的,這里分享給大家,需要的朋友可以參考。下面隨小編一起看看吧。2017-11-11java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實(shí)例
這篇文章主要介紹了java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實(shí)例方法的相關(guān)資料2017-02-02Java微服務(wù)Filter過濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過程詳解
這篇文章主要介紹了Java微服務(wù)Filter過濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過程,首先Sentinel規(guī)則的存儲默認(rèn)是存儲在內(nèi)存的,應(yīng)用重啟之后規(guī)則會丟失。因此我們通過配置中心Nacos保存規(guī)則,然后通過定時拉取Nacos數(shù)據(jù)來獲取規(guī)則配置,可以做到動態(tài)實(shí)時的刷新規(guī)則2023-02-02在Mybatis @Select注解中實(shí)現(xiàn)拼寫動態(tài)sql
這篇文章主要介紹了在Mybatis @Select注解中實(shí)現(xiàn)拼寫動態(tài)sql,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11