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 接口,點(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 文檔的生成方式。在項目中創(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自動生成文檔的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Swagger Api自動生成 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mysql?json類型字段Java+Mybatis數(shù)據(jù)字典功能的實踐方式
這篇文章主要介紹了Mysql?json類型字段Java+Mybatis數(shù)據(jù)字典功能的實踐方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報錯問題
這篇文章主要介紹了MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
SpringBoot整合iText7導(dǎo)出PDF及性能優(yōu)化方式
在SpringBoot項目中整合iText7庫以導(dǎo)出PDF文件,不僅能夠滿足報告生成需求,而且可以處理復(fù)雜的文檔布局與樣式,整合步驟包括添加Maven依賴、編寫PDF生成代碼,性能優(yōu)化方面,建議使用流式處理、緩存樣式與字體、優(yōu)化HTML/CSS結(jié)構(gòu)、采用異步處理2024-09-09
出現(xiàn)java.util.ConcurrentModificationException 問題及解決辦法
這篇文章主要介紹了出現(xiàn)java.util.ConcurrentModificationException 問題及解決辦法的相關(guān)資料,需要的朋友可以參考下2017-02-02
java中用ObjectMapper類實現(xiàn)Json與bean的轉(zhuǎn)換示例
這篇文章主要給大家介紹了關(guān)于在java中用ObjectMapper類實現(xiàn)Json與bean轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08

