springboot整合swagger3和knife4j的詳細(xì)過(guò)程
依賴(lài)
<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‘ 問(wèn)題
knife4j:
enable: true
swagger配置類(lèi)
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 開(kāi)啟生成接口文檔功能
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 = "用戶(hù)管理接口")
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@ApiOperation("添加用戶(hù)")
@ApiImplicitParams({ // 參數(shù)名稱(chēng)
@ApiImplicitParam(name = "userId", value = "用戶(hù)id"),
@ApiImplicitParam(name = "userName", value = "用戶(hù)名", required = true)
})
@PostMapping("add")
public User add(User user) {
userService.addUser(user);
return user;
}
@ApiOperation("查詢(xún)所有用戶(hù)")
@GetMapping("list")
public ResponseResult<User> list() {
return ResponseResult.success(userService.list());
}
@GetMapping("/findOne")
public ResponseResult<User> findOne() {
return ResponseResult.success(userService.findById(1));
}
}測(cè)試
knife4j訪(fǎng)問(wèn)地址 http://localhost:9090/doc.html

swagger訪(fǎng)問(wèn)地址 http://localhost:9090/swagger-ui/index.html

swagger常用注解說(shuō)明
@Api
用在請(qǐng)求的類(lèi)上
@Api(tags = “該類(lèi)的作用進(jìn)行說(shuō)明”)
@ApiOperation
@ApiOperation(value=“改方法的作用”,note=“備注”)
@ApiImplicitParams
@ApiImplicitParams:用在請(qǐng)求的方法上,表示一組參數(shù)說(shuō)明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個(gè)請(qǐng)求參數(shù)的各個(gè)方面
- name:參數(shù)名
- value:參數(shù)的漢字說(shuō)明、解釋
- required:參數(shù)是否必須傳
- paramType:參數(shù)放在哪個(gè)地方
- · header --> 請(qǐng)求參數(shù)的獲?。篅RequestHeader
- · query --> 請(qǐng)求參數(shù)的獲?。篅RequestParam
- · path(用于restful接口)–> 請(qǐng)求參數(shù)的獲?。篅PathVariable
- dataType:參數(shù)類(lèi)型,默認(rèn)String,其它值dataType=“Integer”
- defaultValue:參數(shù)的默認(rèn)值
多個(gè) @ApiImplicitParam 注解需要放在一個(gè) @ApiImplicitParams 注解中
@ApiImplicitParam 注解中雖然可以指定參數(shù)是必填的,但是卻不能代替 @RequestParam(required = true) ,
前者的必填只是在 Swagger 框架內(nèi)必填,拋棄了 Swagger ,這個(gè)限制就沒(méi)用了,所以假如開(kāi)發(fā)者需要指定一個(gè)參數(shù)必填, @RequestParam(required = true) 注解還是不能省略。
@ApiModel
如果參數(shù)是一個(gè)對(duì)象(例如上文的更新接口),對(duì)于參數(shù)的描述也可以放在實(shí)體類(lèi)中。例如下面一段代碼
@ApiModelProperty:用在屬性上,描述類(lèi)的屬性
@ApiModel(value = "用戶(hù)對(duì)象",description = "用戶(hù)表")
public class User {
@Id
@ApiModelProperty(value = "id",required = true)
private int userId;
@ApiModelProperty(value = "用戶(hù)名稱(chēng)",required = true)
private String userName;
}

總結(jié)
到此這篇關(guān)于springboot整合swagger3和knife4j的文章就介紹到這了,更多相關(guān)springboot整合swagger3和knife4j內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot整合Swagger3全注解配置(springdoc-openapi-ui)
- SpringBoot整合Swagger3生成接口文檔過(guò)程解析
- Springboot整合Swagger2后訪(fǎng)問(wèn)swagger-ui.html 404報(bào)錯(cuò)問(wèn)題解決方案
- SpringBoot整合Swagger3生成接口文檔的示例代碼
- Springboot整合Swagger2和Swagger3全過(guò)程
- SpringBoot整合Swagger的方法示例
- SpringBoot整合swagger的操作指南
- SpringBoot整合Swagger接口文檔工具的流程步驟
- SpringBoot整合Swagger教程詳解
- SpringBoot3.x整合swagger的實(shí)現(xiàn)示例
相關(guān)文章
Mybatis?TypeHandler接口及繼承關(guān)系示例解析
這篇文章主要為大家介紹了Mybatis?TypeHandler接口及繼承關(guān)系示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
spring如何使用命名空間p簡(jiǎn)化bean的配置
這篇文章主要介紹了spring如何使用命名空間p簡(jiǎn)化bean的配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Mybatis特殊字符轉(zhuǎn)義查詢(xún)實(shí)現(xiàn)
本文主要介紹了Mybatis特殊字符轉(zhuǎn)義查詢(xún)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
詳解SpringBoot項(xiàng)目的創(chuàng)建與單元測(cè)試
這篇文章主要介紹了詳解SpringBoot項(xiàng)目的創(chuàng)建與單元測(cè)試,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot,感興趣的朋友可以了解下2021-03-03
spring boot 自定義規(guī)則訪(fǎng)問(wèn)獲取內(nèi)部或者外部靜態(tài)資源圖片的方法
這篇文章主要介紹了spring boot 自定義規(guī)則訪(fǎng)問(wèn)獲取內(nèi)部或者外部靜態(tài)資源圖片的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
SpringBoot項(xiàng)目找不到接口報(bào)404錯(cuò)誤的解決辦法
寫(xiě)了一個(gè)簡(jiǎn)單的springboot項(xiàng)目,在啟動(dòng)的時(shí)候idea未報(bào)錯(cuò),瀏覽器訪(fǎng)問(wèn)接口時(shí)報(bào)404的錯(cuò)誤,所以本文給大家介紹了SpringBoot項(xiàng)目找不到接口報(bào)404錯(cuò)誤的解決辦法,文中有相關(guān)的圖文供大家參考,需要的朋友可以參考下2024-12-12
SpringBoot全局Controller返回值格式統(tǒng)一
本文主要介紹了SpringBoot全局Controller返回值格式統(tǒng)一,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
SpringSecurity6.x多種登錄方式配置小結(jié)
SpringSecurity6.x變了很多寫(xiě)法,本文就來(lái)介紹一下SpringSecurity6.x多種登錄方式配置小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Java 創(chuàng)建URL的常見(jiàn)問(wèn)題及解決方案
這篇文章主要介紹了Java 創(chuàng)建URL的常見(jiàn)問(wèn)題及解決方案的相關(guān)資料,需要的朋友可以參考下2016-10-10

