成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

甩掉接口文檔煩惱!Spring Boot 集成 Knife4j,輕松玩轉 API 可視化

開發
今天,我們就來介紹一款神器 Knife4j,它可以幫助你輕松生成美觀、易于使用的 API 文檔,讓你告別文檔編寫煩惱!

一、引言:跟接口文檔說拜拜

作為一名 Java 開發者,你是否還在為編寫繁瑣的 API 文檔而頭疼?傳統的手動編寫方式不僅耗時費力,而且容易出錯,難以維護。今天,我們就來介紹一款神器 Knife4j,它可以幫助你輕松生成美觀、易于使用的 API 文檔,讓你告別文檔編寫煩惱!

二、Knife4j 簡介:API 文檔界的“后起之秀”

簡單來說,Knife4j 是一個集 Swagger2 和 OpenAPI3 為一體的增強解決方案,它不僅擁有 Swagger 的所有功能,還做了很多優化和增強,例如:

  • 界面更美觀:  提供簡潔美觀的界面,方便用戶瀏覽和使用 API 文檔。
  • 功能更豐富:  支持接口排序、搜索、分組等功能,支持多種格式輸出。
  • 易用性更高:  提供了更友好的配置方式,使用起來更加簡單方便。

三、Spring Boot 集成 Knife4j:三步搞定,輕松上手 

1. 添加依賴:引入 Knife4j 包

在你的 Spring Boot 項目的 pom.xml 文件中添加以下依賴:

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
    <version>4.4.0</version>
</dependency>

2. 配置 Knife4j:告訴它你的 API 信息

創建一個配置類,例如 Knife4jConfiguration,使用 @EnableSwagger2WebMvc 注解開啟 Swagger 功能,并配置 Docket Bean,就像這樣:

package com.muqing.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
publicclass Knife4jConfiguration {
    @Bean(value = "mq-admin-api")
    public Docket defaultApi2() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("MQ-ADMIN API文檔")
                        .description("# 基于 Spring Boot 與 Vue3 的后臺管理系統。")
                        .termsOfServiceUrl("https://doc.uyii.cn/")
                        .contact("zhy@uyii.cn")
                        .version("1.0")
                        .build())
                .select()
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
}

3. 運行項目:見證奇跡的時刻

啟動你的 Spring Boot 應用,在瀏覽器中訪問 http://localhost:8800/doc.html,你就能看到 Knife4j 生成的 API 文檔啦!

四、進階用法:玩轉 Knife4j,定制你的 API 文檔

分組管理 API 文檔:

@Bean
public Docket adminApi() {
      returnnew Docket(DocumentationType.SWAGGER_2)
              .groupName("admin")
              .select()
              .apis(RequestHandlerSelectors.basePackage("com.muqing.admin")) // 修改為你的模塊包路徑
              .paths(PathSelectors.any())
              .build();
  }

@Bean
public Docket frontApi() {
      returnnew Docket(DocumentationType.SWAGGER_2)
              .groupName("front")
              .select()
              .apis(RequestHandlerSelectors.basePackage("com.muqing.front")) // 修改為你的模塊包路徑
              .paths(PathSelectors.any())
              .build();
  }

自定義 API 信息:

@RestController
@RequestMapping("/users")
@Api(tags = "用戶管理")
public class UserController {

    @GetMapping("/{id}")
    @ApiOperation("獲取用戶信息")
    public User getUser(@PathVariable Long id) {
        // ...
    }
}

五、實戰演練:小試牛刀,鞏固學習成果

我們來創建一個簡單的 Spring Boot 項目,演示如何使用 Knife4j 生成 API 文檔:

  • 創建一個 Spring Boot 項目,添加 Web 依賴和 Knife4j 依賴。
  • 創建一個 FrontIndexController,編寫接口:
package com.muqing.front.controller;

import com.muqing.common.api.ApiResult;
import com.muqing.front.entity.Article;
import com.muqing.front.service.FrontIndexService;
import com.muqing.front.vo.IndexVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * IndexController
 *
 * @author zhy
 * @since 2023/6/5
 */
@RestController
@Api(tags = "后臺首頁")
@RequestMapping("/front")
publicclass FrontIndexController {

    @Autowired
    private FrontIndexService frontIndexService;

    /**
     * 首頁博主信息
     *
     * @return {@link ApiResult}<{@link IndexVo}>
     */
    @ApiOperation("博主信息")
    @GetMapping()
    public ApiResult<IndexVo> index() {
        IndexVo IndexVo = frontIndexService.getAuthorInfo();
        return ApiResult.success(IndexVo);
    }

    /**
     * 關于我
     *
     * @return {@link Article}
     */
    @ApiOperation("關于我")
    @GetMapping("/about")
    public ApiResult<Article> about() {
        Article article = frontIndexService.getAbout();
        return ApiResult.success(article);
    }
}
  • 運行項目,訪問 http://localhost:8080/doc.html,查看生成的 API 文檔。

結語:API 文檔,從此輕松搞定

Knife4j 為我們提供了一種簡單高效的方式來生成和管理 API 文檔,從此告別繁瑣的手動編寫,讓前后端協作更加順暢! 希望這篇文章能夠幫助你快速上手 Knife4j,更多進階的技能請閱讀官方文檔!

責任編輯:趙寧寧 來源: 源話編程
相關推薦

2021-03-22 08:02:16

WordKnife4jSwagger

2018-10-22 15:34:31

Spring Boo監控視化

2021-07-02 14:07:00

可視化Plotly漏斗圖

2024-10-06 08:35:44

2022-01-26 20:01:24

管理工具knife4j

2018-11-30 15:44:49

可視化圖表數據

2022-02-16 08:21:11

JavaSwagger工具

2025-02-25 11:14:39

2020-10-19 09:46:47

大數據可視化技術

2022-06-28 09:34:24

可視化Python代碼

2020-03-11 14:39:26

數據可視化地圖可視化地理信息

2015-09-14 13:48:35

數據挖掘數據可視化

2018-02-04 22:22:46

大數據開發工具

2024-01-01 09:08:52

API簽名驗簽

2021-12-10 10:46:16

可視化工具LinuxRedis

2020-05-22 13:32:24

可視化詞云圖數據

2016-09-09 13:48:54

API可視化華為開發者社區

2017-03-28 14:57:23

kylinsuperset可視化

2025-03-31 00:44:00

JavaAI開發

2021-07-27 11:45:37

Python 開發編程
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲国产精品99久久久久久久久 | 欧美一级毛片在线播放 | 中文字幕一区二区三区不卡 | 亚洲大片一区 | 久久久久久久久久爱 | 国产一区二区美女 | 在线免费观看日本 | 精品国产成人 | 综合久久av | 欧美国产一区二区三区 | 奇米影视在线 | 欧美日韩亚洲国产综合 | 欧美成人精品一区二区三区 | 视频在线观看一区二区 | 国产精品福利在线 | 久久夜夜| 欧美不卡一区二区三区 | 黄网站免费在线 | 免费在线观看av | 亚洲国产第一页 | 亚洲精品一区二区三区中文字幕 | 在线国产精品一区 | 日本一区二区高清不卡 | 欧美一区二区三区在线 | 久久久久久久亚洲精品 | 一区二区三区视频免费看 | 亚洲电影一区二区三区 | 欧美日韩在线免费 | 久久精品无码一区二区三区 | 婷婷久久五月天 | 中文在线一区 | 一区二区三区在线 | 91免费福利在线 | 黄色在线免费观看视频网站 | 国产羞羞视频在线观看 | 精品一区二区视频 | 久久久久中文字幕 | 91精品国产91久久久久久丝袜 | 亚洲精品中文字幕在线观看 | 搞av.com | 成人免费看片网 |