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

springboot整合swagger3和knife4j的詳細(xì)過程

 更新時間:2022年11月11日 08:59:59   作者:迷失的小鹿  
knife4j的前身是swagger-bootstrap-ui,取名knife4j是希望她能像一把匕首一樣小巧,輕量,并且功能強(qiáng)悍,下面這篇文章主要介紹了springboot整合swagger3和knife4j的詳細(xì)過程,需要的朋友可以參考下

依賴

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
</dependency>

application.yml配置

  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher ## 解決Failed to start bean ‘documentationPluginsBootstrapper‘ 問題
knife4j:
  enable: true

swagger配置類

package com.example.springbooth2.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi//Swagger 開啟生成接口文檔功能
public class SwaggerConfig {

    /**
     * ture 啟用Swagger3.0, false 禁用
     */
    @Value("${knife4j.enable}")
    private Boolean enable;

    @Bean
    Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                //配置網(wǎng)站的基本信息
                .apiInfo(apiInfo())
                .enable(enable)
                .select()
                //指定接口的位置
                // RequestHandlerSelectors.basePackage("com.example.springbooth2.controller") 接口的包所在路徑
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 指定路徑處理PathSelectors.any()代表所有的路徑
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger3") //接口文檔標(biāo)題
                .description("接口文檔") //接口文檔描述
                //作者信息
                // new Contact("作者","作者URL","作者Email")
                .contact(new Contact("jane", "http://www.baidu.com", "123@qq.com"))
                .version("1.0")//版本
                .build();
    }
}

controller

package com.example.springbooth2.controller;

import com.example.springbooth2.config.ResponseResult;
import com.example.springbooth2.entity.User;
import com.example.springbooth2.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

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

    @Resource
    private UserService userService;

    @ApiOperation("添加用戶")
    @ApiImplicitParams({ // 參數(shù)名稱
            @ApiImplicitParam(name = "userId", value = "用戶id"),
            @ApiImplicitParam(name = "userName", value = "用戶名",  required = true)
    })
    @PostMapping("add")
    public User add(User user) {
        userService.addUser(user);
        return user;
    }

    @ApiOperation("查詢所有用戶")
    @GetMapping("list")
    public ResponseResult<User> list() {
        return ResponseResult.success(userService.list());
    }

    @GetMapping("/findOne")
    public ResponseResult<User> findOne() {
        return ResponseResult.success(userService.findById(1));
    }
}

測試

knife4j訪問地址 http://localhost:9090/doc.html

swagger訪問地址 http://localhost:9090/swagger-ui/index.html

swagger常用注解說明

@Api

用在請求的類上

@Api(tags = “該類的作用進(jìn)行說明”)

@ApiOperation

@ApiOperation(value=“改方法的作用”,note=“備注”)

@ApiImplicitParams

@ApiImplicitParams:用在請求的方法上,表示一組參數(shù)說明

@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數(shù)的各個方面

  • name:參數(shù)名
  • value:參數(shù)的漢字說明、解釋
  • required:參數(shù)是否必須傳
  • paramType:參數(shù)放在哪個地方
  • · header --> 請求參數(shù)的獲?。篅RequestHeader
  • · query --> 請求參數(shù)的獲取:@RequestParam
  • · path(用于restful接口)–> 請求參數(shù)的獲?。篅PathVariable
  • dataType:參數(shù)類型,默認(rèn)String,其它值dataType=“Integer”
  • defaultValue:參數(shù)的默認(rèn)值

多個 @ApiImplicitParam 注解需要放在一個 @ApiImplicitParams 注解中

@ApiImplicitParam 注解中雖然可以指定參數(shù)是必填的,但是卻不能代替 @RequestParam(required = true) ,

前者的必填只是在 Swagger 框架內(nèi)必填,拋棄了 Swagger ,這個限制就沒用了,所以假如開發(fā)者需要指定一個參數(shù)必填, @RequestParam(required = true) 注解還是不能省略。

@ApiModel

如果參數(shù)是一個對象(例如上文的更新接口),對于參數(shù)的描述也可以放在實體類中。例如下面一段代碼
@ApiModelProperty:用在屬性上,描述類的屬性

@ApiModel(value = "用戶對象",description = "用戶表")
public class User {
    @Id
    @ApiModelProperty(value = "id",required = true)
    private int userId;
    @ApiModelProperty(value = "用戶名稱",required = true)
    private String userName;
}

 總結(jié)

到此這篇關(guān)于springboot整合swagger3和knife4j的文章就介紹到這了,更多相關(guān)springboot整合swagger3和knife4j內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談Java中幾個常用集合添加元素的效率

    淺談Java中幾個常用集合添加元素的效率

    下面小編就為大家?guī)硪黄獪\談Java中幾個常用集合添加元素的效率。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Java Builder Pattern建造者模式詳解及實例

    Java Builder Pattern建造者模式詳解及實例

    這篇文章主要介紹了Java Builder Pattern建造者模式詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Java?JDK內(nèi)置常用接口和深淺拷貝

    Java?JDK內(nèi)置常用接口和深淺拷貝

    這篇文章主要介紹了Java?JDK內(nèi)置常用接口和深淺拷貝,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06
  • JAVA OutputStreamWriter流的實現(xiàn)

    JAVA OutputStreamWriter流的實現(xiàn)

    OutputStreamWriter是從字符流到字節(jié)流的橋接,它使用的字符集可以通過名稱指定,也可以明確指定,或者可以接受平臺的默認(rèn)字符集,本文詳細(xì)的介紹了JAVA OutputStreamWriter流的使用,感興趣的可以了解一下
    2021-06-06
  • Java中ArrayList與順序表的概念與使用實例

    Java中ArrayList與順序表的概念與使用實例

    順序表是指用一組地址連續(xù)的存儲單元依次存儲各個元素,使得在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲在相鄰的物理存儲單元中的線性表,下面這篇文章主要介紹了Java?ArrayList與順序表的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • SpringBoot+Vue中的Token續(xù)簽機(jī)制

    SpringBoot+Vue中的Token續(xù)簽機(jī)制

    本文主要介紹了SpringBoot+Vue中的Token續(xù)簽機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • Feign如何實現(xiàn)第三方的HTTP請求

    Feign如何實現(xiàn)第三方的HTTP請求

    這篇文章主要介紹了Feign如何實現(xiàn)第三方的HTTP請求,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 利用maven引入第三方j(luò)ar包以及打包

    利用maven引入第三方j(luò)ar包以及打包

    Maven是通過倉庫對依賴進(jìn)行管理的,當(dāng)Maven項目需要某個依賴時,只要其POM中聲明了依賴的坐標(biāo)信息,Maven就會自動從倉庫中去下載該構(gòu)件使用,如何將jar引用到項目,并且能夠讓項目正常調(diào)用該jar包的方法,本篇文章重點針對于這兩點進(jìn)行講解
    2023-05-05
  • 淺談Java中Spring Boot的優(yōu)勢

    淺談Java中Spring Boot的優(yōu)勢

    在本篇文章中小編給大家分析了Java中Spring Boot的優(yōu)勢以及相關(guān)知識點內(nèi)容,興趣的朋友們可以學(xué)習(xí)參考下。
    2018-09-09
  • Spring?Bean中的六種作用域你了解嗎

    Spring?Bean中的六種作用域你了解嗎

    Bean的作用域是指Bean實例的生命周期及可見性范圍,Spring框架定義了6種作用域,本文就來和大家聊聊這6種作用域的定義與使用,希望對大家有所幫助
    2023-09-09

最新評論