国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

knife4j接口文檔

這篇具有很好參考價值的文章主要介紹了knife4j接口文檔。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

knife4j是為Java MVC框架集成Swagger生成Api文檔的增強解決方案,前身是swagger-bootstrap-ui,取名knife4j是希望它能像一把匕首一樣小巧,輕量,并且功能強悍!其底層是對Springfox的封裝,使用方式也和Springfox一致,只是對接口文檔UI進行了優(yōu)化。

核心功能

  • 文檔說明:根據(jù)Swagger的規(guī)范說明,詳細列出接口文檔的說明,包括接口地址、類型、請求示例、請求參數(shù)、響應示例、響應參數(shù)、響應碼等信息,對該接口的使用情況一目了然。

  • 在線調試:提供在線接口聯(lián)調的強大功能,自動解析當前接口參數(shù),同時包含表單驗證,調用參數(shù)可返回接口響應內容、headers、響應時間、響應狀態(tài)碼等信息,幫助開發(fā)者在線調試。

入門案例:

1.創(chuàng)建spring-boot工程knife4j_demo并配置pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<groupId>com.example</groupId>
	<artifactId>knife4j_demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>knife4j_demo</name>
	<description>Demo project for Spring Boot</description>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>knife4j-spring-boot-starter</artifactId>
			<version>2.0.1</version>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
	</dependencies>
</project>

?從依賴關系中也能看出它是對Springfox的封裝,因此使用方式一直

knife4j接口文檔

2.創(chuàng)建實體類User和Order和接口UserController和OrderController,并使用swagger的注解

3.創(chuàng)建配置屬性類SwaggerProperties

package com.example.config;


import lombok.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/*
 *配置屬性類,用于封裝接口文檔相關屬性,從配置文件讀取信息封裝成當前對象
 */

@Data
@ConfigurationProperties(prefix = "myknife4j.swagger")
public class SwaggerProperties {
    private String title = "在線文檔"; //標題
    private String group = ""; //自定義組名
    private String description = "在線文檔"; //描述
    private String version = "1.0"; //版本
    private Contact contact = new Contact(); //聯(lián)系人
    private String basePackage = ""; //swagger會解析的包路徑
    private List<String> basePath = new ArrayList<>(); //swagger會解析的url規(guī)則
    private List<String> excludePath = new ArrayList<>();//在basePath基礎上需要排除的url規(guī)則
    private Map<String, DocketInfo> docket = new LinkedHashMap<>(); //分組文檔
    public String getGroup() {
        if (group == null || "".equals(group)) {
            return title;
        }
        return group;
    }
    @Data
    public static class DocketInfo {
        private String title = "在線文檔"; //標題
        private String group = ""; //自定義組名
        private String description = "在線文檔"; //描述
        private String version = "1.0"; //版本
        private Contact contact = new Contact(); //聯(lián)系人
        private String basePackage = ""; //swagger會解析的包路徑
        private List<String> basePath = new ArrayList<>(); //swagger會解析的url規(guī)則
        private List<String> excludePath = new ArrayList<>();//在basePath基礎上需要排除的url
        public String getGroup() {
            if (group == null || "".equals(group)) {
                return title;
            }
            return group;
        }
    }
    @Data
    public static class Contact {
        private String name = ""; //聯(lián)系人
        private String url = ""; //聯(lián)系人url
        private String email = ""; //聯(lián)系人email
    }
}

4.創(chuàng)建配置類SwaggerAutoConfiguration

package com.example.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Configuration
@ConditionalOnProperty(name = "myknife4j.swagger.enabled", havingValue = "true",
        matchIfMissing = true)
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration implements BeanFactoryAware {
    @Autowired
    SwaggerProperties swaggerProperties;
    private BeanFactory beanFactory;
    @Bean
    @ConditionalOnMissingBean
    public List<Docket> createRestApi(){
        ConfigurableBeanFactory configurableBeanFactory =
                (ConfigurableBeanFactory) beanFactory;
        List<Docket> docketList = new LinkedList<>();
        // 沒有分組
        if (swaggerProperties.getDocket().isEmpty()) {
            Docket docket = createDocket(swaggerProperties);
            configurableBeanFactory.registerSingleton(swaggerProperties.getTitle(),
                    docket);
            docketList.add(docket);
            return docketList;
        }
        // 分組創(chuàng)建
        for (String groupName : swaggerProperties.getDocket().keySet()){
            SwaggerProperties.DocketInfo docketInfo =
                    swaggerProperties.getDocket().get(groupName);
            ApiInfo apiInfo = new ApiInfoBuilder()
                    //頁面標題
                    .title(docketInfo.getTitle())
                    //創(chuàng)建人
                    .contact(new Contact(docketInfo.getContact().getName(),
                            docketInfo.getContact().getUrl(),
                            docketInfo.getContact().getEmail()))
                    //版本號
                    .version(docketInfo.getVersion())
                    //描述
                    .description(docketInfo.getDescription())
                    .build();

            // base-path處理
            // 當沒有配置任何path的時候,解析/**
            if (docketInfo.getBasePath().isEmpty()) {
                docketInfo.getBasePath().add("/**");
            }
            List<Predicate<String>> basePath = new ArrayList<>();
            for (String path : docketInfo.getBasePath()) {
                basePath.add(PathSelectors.ant(path));
            }

            // exclude-path處理
            List<Predicate<String>> excludePath = new ArrayList<>();
            for (String path : docketInfo.getExcludePath()) {
                excludePath.add(PathSelectors.ant(path));
            }

            Docket docket = new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo)
                    .groupName(docketInfo.getGroup())
                    .select()
                    //為當前包路徑
                    .apis(RequestHandlerSelectors.basePackage(docketInfo.getBasePackage()))
                    .paths(Predicates.and(Predicates.not(Predicates.or(excludePath)),Predicates.or(basePath)))
                    .build();
            configurableBeanFactory.registerSingleton(groupName, docket);
            docketList.add(docket);
        }
        return docketList;
    }

    //構建 api文檔的詳細信息
    private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
        return new ApiInfoBuilder()
                //頁面標題
                .title(swaggerProperties.getTitle())
                //創(chuàng)建人
                .contact(new Contact(swaggerProperties.getContact().getName(),
                        swaggerProperties.getContact().getUrl(),
                        swaggerProperties.getContact().getEmail()))
                //版本號
                .version(swaggerProperties.getVersion())
                //描述
                .description(swaggerProperties.getDescription())
                .build();
    }

    //創(chuàng)建接口文檔對象
    private Docket createDocket(SwaggerProperties swaggerProperties) {
        //API 基礎信息
        ApiInfo apiInfo = apiInfo(swaggerProperties);

        // base-path處理
        // 當沒有配置任何path的時候,解析/**
        if (swaggerProperties.getBasePath().isEmpty()) {
            swaggerProperties.getBasePath().add("/**");
        }
        List<Predicate<String>> basePath = new ArrayList<>();
        for (String path : swaggerProperties.getBasePath()) {
            basePath.add(PathSelectors.ant(path));
        }

        // exclude-path處理
        List<Predicate<String>> excludePath = new ArrayList<>();
        for (String path : swaggerProperties.getExcludePath()) {
            excludePath.add(PathSelectors.ant(path));
        }

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .groupName(swaggerProperties.getGroup())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                .paths(Predicates.and(Predicates.not(Predicates.or(excludePath)),Predicates.or(basePath)))
                .build();
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
}

5.配置application.yml文件

server:
  port: 7788
# 對應的SwaggerProperties配置類中的屬性
myknife4j:
  swagger:
    enabled: true #是否啟用swagger
    title: 測試標題
    description: 測試文檔
    docket:
      controller: # 對應的SwaggerProperties配置類中的名為docket的map集合的key
        title: controller模塊
        description: controller模塊測試文檔
        contact:
          name: xxx
          url: www.xxx.com
          email: xxxxx.@xx.com
        base-package: com.example.controller

6.執(zhí)行啟動類main方法啟動項目,訪問地址:http://服務地址:端口/doc.html

knife4j接口文檔

如果接口文檔不分組,我們可以修改application.yml文件:

server:
  port: 7788
# 對應的SwaggerProperties配置類中的屬性
myknife4j:
  swagger:
    enabled: true #是否啟用swagger
    title: test模塊
    base-package: com.example.controller

knife4j接口文檔文章來源地址http://www.zghlxwxcb.cn/news/detail-403288.html

到了這里,關于knife4j接口文檔的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 【SpringBoot】SpringBoot引入接口文檔生成工具(Swagger+Knife4j)

    由于目前工作項目寫的接口越來越多了,為了能夠更加方便地優(yōu)化接口,以及整理接口文檔,所以就考慮引入接口文檔生成工具。目前的接口文檔生成工具被提及較多的是Swagger,經(jīng)過了引入嘗試后,Swagger是比較輕松地就被引入了。但是Swagger頁面屬實是難以恭維,比較簡單但

    2024年01月23日
    瀏覽(21)
  • Spring Boot 集成 API 文檔 - Swagger、Knife4J、Smart-Doc

    Spring Boot 集成 API 文檔 - Swagger、Knife4J、Smart-Doc

    Swagger 作為 API 設計和文檔的強大工具,是一個由專門的工具集合支持的框架,它在整個 API 的生命周期中發(fā)揮作用,從設計和文檔,到測試和部署。通過提供可視化界面,Swagger 讓開發(fā)人員和最終用戶都能清晰地理解和操作 API。 使用建議:筆者建議優(yōu)先考慮 Knife4J,它已經(jīng)能

    2024年01月22日
    瀏覽(22)
  • Spring Cloud Gateway 網(wǎng)關整合 Knife4j 4.3 實現(xiàn)微服務接口文檔聚合

    Spring Cloud Gateway 網(wǎng)關整合 Knife4j 4.3 實現(xiàn)微服務接口文檔聚合

    ?? 作者主頁: 有來技術 ?? 開源項目: youlai-mall ?? vue3-element-admin ?? youlai-boot ?? 倉庫主頁: Gitee ?? Github ?? GitCode ?? 歡迎點贊 ?? 收藏 ?留言 ?? 如有錯誤敬請糾正! youlai-mall 開源微服務商城新版本基于 Spring Boot 3 和 Java 17,同時采用 Knife4j 4.3。與以前版本不同的是

    2024年02月05日
    瀏覽(23)
  • Spring Cloud Gateway + Knife4j 4.3 實現(xiàn)微服務網(wǎng)關聚合接口文檔

    Spring Cloud Gateway + Knife4j 4.3 實現(xiàn)微服務網(wǎng)關聚合接口文檔

    ?? 作者主頁: 有來技術 ?? 開源項目: youlai-mall ?? vue3-element-admin ?? youlai-boot ?? 倉庫主頁: Gitee ?? Github ?? GitCode ?? 歡迎點贊 ?? 收藏 ?留言 ?? 如有錯誤敬請糾正! youlai-mall 開源微服務商城新版本基于 Spring Boot 3 和 Java 17,同時采用 Knife4j 4.3。與以前版本不同的是

    2024年02月08日
    瀏覽(22)
  • Knife4j文檔請求異常(更新)

    Knife4j文檔請求異常(更新)

    在SpringBoot項目中,如果是分不同的模塊開發(fā)。 注解配置 @EnableSwagger2WebMvc 不在啟動類上,而是加到了其他模塊的注解中,可能會導致這種情況發(fā)生。 我的是common一個單獨的模塊,在common模塊中配置了WebMvcConfig。 然后在WebMvcConfig類上面加了注解 @EnableSwagger2WebMvc . 那么,解決方

    2024年01月18日
    瀏覽(38)
  • Knife4j框架介紹

    Knife4j框架介紹

    Knife4j是一款基于Swagger 2的在線API文檔框架,是日常開發(fā)中很常用的框架,基于此框架,后端可以和前端開發(fā)人員進行高效溝通。 使用Knife4j框架只需要以下三步: 1:添加Knife4j的依賴 (當前建議使用的Knife4j版本,只適用于Spring Boot 2.6以下版本,不含Spring Boot 2.6) 2:在主配置

    2024年02月13日
    瀏覽(18)
  • springboot集成knife4j詳細教程

    springboot集成knife4j詳細教程

    使用原生的swagger作為接口文檔,功能不夠強大,并且默認的ui比較簡陋,不符合大眾審美。所以實際開發(fā)中推薦使用knife4j對swagger進行增強。knife4j的地址:https://gitee.com/xiaoym/knife4j 想要使用knife4j非常簡單,只要在Springboot項目中引入knife4j的依賴即可 注意:引入knife4j后會自動

    2024年02月20日
    瀏覽(22)
  • 【Java - 框架 - Knife4j】隨筆

    【Java - 框架 - Knife4j】隨筆

    項目文件; 【1】 【2】 【3】

    2024年01月21日
    瀏覽(18)
  • 全網(wǎng)多種方式解決Knife4j文檔請求異常

    全網(wǎng)多種方式解決Knife4j文檔請求異常

    今天在本地啟動項目后,刷新 Knife4j 接口文檔,卻報出如下錯誤: 即 Knife4j文檔請求異常 。 報出 Knife4j文檔請求異常 錯誤時,趕緊打開控制臺,如下圖所示: 控制臺提示為 Unchecked runtime.lastError: The message port closed before a response was received. 簡單理解就是 端口號關閉 了,于是查

    2023年04月08日
    瀏覽(16)
  • knife4j實現(xiàn)微服務swagger文檔聚合

    knife4j實現(xiàn)微服務swagger文檔聚合

    在項目開發(fā)過程中,接口文檔的使用是在所難免的,但是在微服務場景下,多個服務之間的swagger是分散的,雖然swagger提供了微服務的聚合方式,配置過于繁瑣,加之swagger本身的功能比較少,而且ui布局也比較蛋痛,此處推薦一款新框架用于增強swagger以及實現(xiàn)微服務接口文檔的聚合 kni

    2024年02月13日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包