欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合Swagger Api自動生成文檔的實(shí)現(xiàn)

 更新時間:2023年06月12日 08:42:21   作者:Cosolar  
本文主要介紹了SpringBoot整合Swagger Api自動生成文檔的實(shí),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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)文章

最新評論