SpringBoot整合Swagger Api自動生成文檔的實現(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 項目中創(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 文檔的生成方式。在項目中創(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 接口,點擊接口后可以看到該接口的詳細信息。
在 Swagger 頁面上,我們還可以進行接口測試和調(diào)試。在 /hello 接口上,點擊 "Try it out" 按鈕,填寫請求參數(shù)后點擊 "Execute" 按鈕,即可發(fā)送請求并獲取響應(yīng)。
通過整合 Swagger,我們可以在 Spring Boot 應(yīng)用程序中快速生成 RESTful API 文檔,并且直接在頁面上進行調(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 文檔的生成方式。在項目中創(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 界面,并且可以直接在頁面上進行接口測試和調(diào)試,非常方便。
通過整合 Knife4j,我們可以在 Spring Boot 應(yīng)用程序中快速生成 RESTful API 文檔,并且直接在頁面上進行調(diào)試和測試,提高了開發(fā)效率。
到此這篇關(guān)于SpringBoot整合Swagger Api自動生成文檔的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Swagger Api自動生成 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用Validation自定義Double類型屬性校驗
這篇文章主要為大家詳細介紹了Java如何使用Validation自定義Double類型屬性校驗,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-11-11
Spring Boot中如何使用Convert接口實現(xiàn)類型轉(zhuǎn)換器
這篇文章主要介紹了Spring Boot中使用Convert接口實現(xiàn)類型轉(zhuǎn)換器的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Java docx4j高效處理Word文檔的實戰(zhàn)指南
對于需要在Java應(yīng)用程序中生成、修改或處理Word文檔的開發(fā)者來說,docx4j是一個強大而專業(yè)的選擇,下面我們就來看看docx4j的具體使用吧2025-07-07
springboot添加多數(shù)據(jù)源的方法實例教程
這篇文章主要給大家介紹了關(guān)于springboot添加多數(shù)據(jù)源方法的相關(guān)資料,在實際開發(fā)中經(jīng)??赡苡龅皆谝粋€應(yīng)用中可能要訪問多個數(shù)據(jù)庫多的情況,需要的朋友可以參考下2023-09-09
SpringCloud全面解析@FeignClient標(biāo)識接口的過程
這篇文章主要介紹了SpringCloud全面解析@FeignClient標(biāo)識接口的過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringCloud Gateway鑒權(quán)和跨域解決方案
網(wǎng)關(guān)是介于客戶端和服務(wù)器端之間的中間層,所有的外部請求都會先經(jīng)過 網(wǎng)關(guān)這一層,也就是說,API 的實現(xiàn)方面更多的考慮業(yè)務(wù)邏輯,而安全、性能、監(jiān)控可以交由 網(wǎng)關(guān)來做,這樣既提高業(yè)務(wù)靈活性又不缺安全性,本文給大家介紹SpringCloud Gateway鑒權(quán)和跨域解決方案,一起看看吧2023-11-11

