溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×

SpringBoot集成Swagger如何添加maven依賴

發(fā)布時(shí)間:2021-03-22 11:17:07 來源:億速云 閱讀:1858 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了SpringBoot集成Swagger如何添加maven依賴,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

SpringBoot集成Swagger 添加maven依賴

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>

 <dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
 </dependency>

要求:jdk 1.8 + 否則swagger2無法運(yùn)行 要使用Swagger,我們需要編寫一個(gè)配置類-SwaggerConfig來配置 Swagger

package com.yf.exam.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

  //配置了swagger的Docket 的 bean 實(shí)例
  @Bean
  public Docket docket(){
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo());
  }
  //配置 swagger 信息 = apiInfo
  private ApiInfo apiInfo(){
    //作者信息
    Contact contact = new Contact("瀟七", "https://www.xhost.vip/", "2278023068@qq.com");
    return new ApiInfo("API文檔",
        "接口信息",
        "v1.0",
        
        contact,
        "Apache 2.0",
        "http://www.apache.org/licenses/LICENSE-2.0",
        new ArrayList()
    );
  }
}

訪問測試 :http://localhost:8080/swagger-ui.html ,可以看到swagger的界面;
-

SpringBoot集成Swagger如何添加maven依賴

knife4j

官網(wǎng)參考地址:knife4j
knife4j是為Java MVC框架集成Swagger生成Api文檔的增強(qiáng)解決方案(在非Java項(xiàng)目中也提供了前端UI的增強(qiáng)解決方案),前身是swagger-bootstrap-ui,取名knife4j是希望她能像一把匕首一樣小巧,輕量,并且功能強(qiáng)悍!

簡潔

基于左右菜單式的布局方式,是更符合國人的操作習(xí)慣吧.文檔更清晰…

個(gè)性化配置

個(gè)性化配置項(xiàng),支持接口地址、接口description屬性、UI增強(qiáng)等個(gè)性化配置功能…

增強(qiáng)

接口排序、Swagger資源保護(hù)、導(dǎo)出Markdown、參數(shù)緩存眾多強(qiáng)大功能.

SpringBoot集成Knife4j 添加maven依賴

<dependency>
      <groupId>com.github.xiaoymin</groupId>
      <artifactId>knife4j-spring-boot-starter</artifactId>
      <!--在引用時(shí)請?jiān)趍aven中央倉庫搜索最新版本號(hào)-->
      <version>2.0.4</version>
 </dependency>

-訪問測試 :http://localhost:8080/doc.html ,可以看到knife4j的界面;

SpringBoot集成Swagger如何添加maven依賴

離線文檔導(dǎo)出

Knife4j提供導(dǎo)出4種格式的離線文檔(Html\Markdown\Word\Pdf)

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“SpringBoot集成Swagger如何添加maven依賴”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI