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

SpringBoot中整合knife4j接口文檔的實(shí)踐

 更新時(shí)間:2020年09月10日 10:06:23   作者:Asurplus、  
這篇文章主要介紹了SpringBoot中整合knife4j接口文檔的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

在項(xiàng)目開發(fā)中,web項(xiàng)目的前后端分離開發(fā),APP開發(fā),需要由前后端工程師共同定義接口,編寫接口文檔,之后大家都根據(jù)這個(gè)接口文檔進(jìn)行開發(fā),到項(xiàng)目結(jié)束前都要一直維護(hù)

接口文檔使得項(xiàng)目開發(fā)過程中前后端工程師有一個(gè)統(tǒng)一的文件進(jìn)行溝通交流開發(fā),項(xiàng)目維護(hù)中或者項(xiàng)目人員更迭,方便后期人員查看、維護(hù)

一、界面先賞

1、首頁

2、接口文檔

3、調(diào)試

二、整合 knife4j

1、引入 maven 依賴

<!-- knife4j接口文檔 start -->
<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>knife4j-spring-boot-starter</artifactId>
  <version>2.0.2</version>
</dependency>
<!-- 避免版本沖突 -->
<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>29.0-jre</version>
</dependency>

一般情況我們只需要引入 knife4j 的依賴即可,但是有時(shí)會(huì)出現(xiàn) guava 的版本沖突,所以,我們把 guava 一起引入進(jìn)來

2、knife4j 配置文件

創(chuàng)建 Knife4jConfig 文件

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.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * knife4j 配置
 *
 * @Author Lizhou
 */
@Configuration
@EnableSwagger2
public class Knife4jConfig {

  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.zyxx"))
        .paths(PathSelectors.any())
        .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("SpringBoot項(xiàng)目 后臺(tái)服務(wù)API接口文檔")
        .description("使用 knife4j 搭建的后臺(tái)服務(wù)API接口文檔")
        .termsOfServiceUrl("http://localhost:8080/")
        .contact("lizhou")
        .version("1.0.0")
        .build();
  }
}

整體配置與 Swagger2 幾乎一致,掃描 controller 所在的包

3、啟動(dòng)類

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.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * knife4j 配置
 *
 * @Author Lizhou
 */
@Configuration
@EnableSwagger2
public class Knife4jConfig {

  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.zyxx"))
        .paths(PathSelectors.any())
        .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("SpringBoot項(xiàng)目 后臺(tái)服務(wù)API接口文檔")
        .description("使用 knife4j 搭建的后臺(tái)服務(wù)API接口文檔")
        .termsOfServiceUrl("http://localhost:8080/")
        .contact("lizhou")
        .version("1.0.0")
        .build();
  }
}

我們需要開放其靜態(tài)資源的訪問,啟動(dòng)類實(shí)現(xiàn) WebMvcConfigurer 接口,重寫 addResourceHandlers 方法

4、訪問文檔

啟動(dòng)項(xiàng)目,訪問路徑http://localhost:8080/doc.html

三、注意

訪問時(shí),如果提示 knife4j 文檔異常,檢查下自己的攔截器是否沒有放開 knife4j 所需要的請(qǐng)求


需要在攔截器,放開請(qǐng)求

package com.zyxx.common.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 注冊(cè)攔截器
 *
 * @Author Lizhou
 **/
@Configuration
public class WebConfigurer implements WebMvcConfigurer {

  @Autowired
  private LoginInterceptor loginHandlerInterceptor;

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    InterceptorRegistration ir = registry.addInterceptor(loginHandlerInterceptor);
    // 攔截路徑
    ir.addPathPatterns("/*");
    // 不攔截路徑
    List<String> irs = new ArrayList<String>();
    irs.add("/login");
    irs.add("/api/*");
    // 開放knife4j
    irs.add("/doc.html");
    irs.add("/service-worker.js");
    irs.add("/swagger-resources");
    ir.excludePathPatterns(irs);
  }
}

四、使用

使用注解的方式與 swagger2 是一樣的

1、controller

import com.zyxx.common.utils.ResponseResult;
import com.zyxx.sbm.service.MgtUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 用戶信息表 前端控制器
 * </p>
 *
 * @author lizhou
 * @since 2020-06-30
 */
@Api(tags = "后臺(tái)管理端--用戶模塊")
@Controller
@RequestMapping("/mgt-user")
public class MgtUserController {

  @Autowired
  private MgtUserService mgtUserService;

  @ApiOperation(value = "分頁查詢用戶數(shù)據(jù)", notes = "分頁查詢用戶數(shù)據(jù)")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "page", value = "頁碼數(shù)", required = true),
      @ApiImplicitParam(name = "limit", value = "每頁條數(shù)", required = true)
  })
  @GetMapping("list")
  @ResponseBody
  public ResponseResult listUser(int page, int limit) {
    return mgtUserService.listUser(page, limit);
  }
}

@Api,整個(gè)類的注釋
@ApiOperation,方法上的注釋
@ApiImplicitParams,參數(shù)列表的注釋
@ApiImplicitParam,每一個(gè)參數(shù)的注釋

2、實(shí)體類

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 用戶信息表
 * </p>
 *
 * @author lizhou
 * @since 2020-06-30
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="mgt_user對(duì)象", description="用戶信息對(duì)象")
public class MgtUser extends Model<MgtUser> {

  private static final long serialVersionUID = 1L;

  @TableId(value = "id", type = IdType.AUTO)
  @ApiModelProperty(value = "主鍵ID")
  private Long id;

  @ApiModelProperty(value = "姓名")
  private String name;

  @ApiModelProperty(value = "年齡")
  private Integer age;

  @ApiModelProperty(value = "技能")
  private String skill;

  @ApiModelProperty(value = "評(píng)價(jià)")
  private String evaluate;

  @ApiModelProperty(value = "分?jǐn)?shù)")
  private Long fraction;

  @Override
  protected Serializable pkVal() {
    return this.id;
  }
}

@ApiModel,實(shí)體類的注解
@ApiModelProperty,字段的注解

到此這篇關(guān)于SpringBoot中整合knife4j接口文檔的實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot整合knife4j內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java類初始化順序詳解

    Java類初始化順序詳解

    這篇文章主要介紹了Java類初始化順序詳解,java語言在使用過程中最先開始就是初始化,在工作中如果遇到什么問題需?要定位往往到最后也可能是初始化的問題,因此掌握初始化的順序很重要,需要的朋友可以參考下
    2023-08-08
  • Mybatis mapper配置文件xml存放位置

    Mybatis mapper配置文件xml存放位置

    這篇文章主要介紹了Mybatis mapper配置文件xml存放位置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-12-12
  • MyBatis攔截器實(shí)現(xiàn)分頁功能實(shí)例

    MyBatis攔截器實(shí)現(xiàn)分頁功能實(shí)例

    本篇文章主要介紹了MyBatis攔截器實(shí)現(xiàn)分頁功能實(shí)例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。
    2017-04-04
  • Java中Runnable與Callable接口的區(qū)別詳解

    Java中Runnable與Callable接口的區(qū)別詳解

    這篇文章主要為大家詳細(xì)介紹了Java中Runnable與Callable接口的區(qū)別,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下
    2023-03-03
  • springboot+spring?data?jpa實(shí)現(xiàn)新增及批量新增方式

    springboot+spring?data?jpa實(shí)現(xiàn)新增及批量新增方式

    這篇文章主要介紹了springboot+spring?data?jpa實(shí)現(xiàn)新增及批量新增方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Android、iOS和Java通用的AES128加密解密示例代碼

    Android、iOS和Java通用的AES128加密解密示例代碼

    現(xiàn)在很多App在與服務(wù)器接口的請(qǐng)求和響應(yīng)過程中,為了安全都會(huì)涉及到加密和解密的問題,如果不加的話就會(huì)是明文的,即使加了GZIP也可以被直接解壓成明文。如果同時(shí)有Android和IOS的App的話、必須要保證加密和解密的算法一致、不然后臺(tái)沒法處理,下面通過這篇文章學(xué)習(xí)下。
    2016-11-11
  • Java數(shù)組,去掉重復(fù)值、增加、刪除數(shù)組元素的方法

    Java數(shù)組,去掉重復(fù)值、增加、刪除數(shù)組元素的方法

    下面小編就為大家?guī)硪黄狫ava數(shù)組,去掉重復(fù)值、增加、刪除數(shù)組元素的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-10-10
  • spring boot空屬性賦值問題與aspect日志實(shí)現(xiàn)方法

    spring boot空屬性賦值問題與aspect日志實(shí)現(xiàn)方法

    這篇文章主要介紹了spring boot空屬性賦值問題與aspect日志實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • spring boot配置ssl實(shí)現(xiàn)HTTPS的方法

    spring boot配置ssl實(shí)現(xiàn)HTTPS的方法

    這篇文章主要介紹了spring boot配置ssl實(shí)現(xiàn)HTTPS的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-03-03
  • springboot使用filter獲取自定義請(qǐng)求頭的實(shí)現(xiàn)代碼

    springboot使用filter獲取自定義請(qǐng)求頭的實(shí)現(xiàn)代碼

    這篇文章主要介紹了springboot使用filter獲取自定義請(qǐng)求頭的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05

最新評(píng)論