knife4j?整合?springboot的過程詳解
官方文檔:https://doc.xiaominfo.com/knife4j
版本兼容說明:https://doc.xiaominfo.com/docs/quick-start/start-knife4j-version
升級說明:https://doc.xiaominfo.com/docs/upgrading/upgrading-to-v4
版本兼容慣關(guān)系:
springboot 1.5.x~2.0.0 對應(yīng) <Knife4j 2.0.0
springboot 2.0 ~ 2.2 對應(yīng) Knife4j 2.0.0 ~ 2.0.6
springboot 2.2.x~2.4.0 對應(yīng) Knife4j 2.0.6 ~ 2.0.9
springboot 2.4.0~2.7.x 對應(yīng) >=Knife4j 4.0.0
1.引入依賴:
<!-- knife4j-spring-boot-starter:(3.0 ~ 3.0.3 是過度版本,官方不建議使用) -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.9</version>
</dependency>注意:本次整合springboot版本為2.3.12
2.配置類
Configuration
@EnableKnife4j
@EnableSwagger2WebMvc // 如果是 knife4j 3.x版本,則只需要去除掉該注解即可
public class SwaggerConfig {
private String basePackage = "com.xxx.xxx.controller";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.groupName("api")
.enable(true)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(basePackage)) // 基于包掃描
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // 基于注解
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API 接口文檔")
.description("Restful API 接口文檔")
.version("1.0")
.contact(new Contact("聯(lián)系人姓名","聯(lián)系人url","聯(lián)系人email"))
.termsOfServiceUrl("服務(wù)條款URL")
.license("xxx License Version 1.0")
.licenseUrl("http://www.xxx.xxx/licenses/LICENSE-1.0")
.build();
}
}3.配置文件
# 版本建議:3.0~3.0.3 底層依賴springfox框架版本升級至3.0.3,OpenAPI規(guī)范是v3,過度版本,建議開發(fā)者不要使用 knife4j.enable=true # 是否開啟生產(chǎn)環(huán)境屏蔽 true:關(guān)閉swagger,false:開啟swagger # true - 設(shè)置未true報錯:You do not have permission to access this page - 即生產(chǎn)環(huán)境禁止訪問 knife4j.production=false knife4j.setting.language=zh-CN
4.編寫代碼Controller
@Api(tags = "測試接口")
@Controller
@RequestMapping("/test")
public class TestController {
@Autowired
private RedisTemplate redisTemplate;
@ApiOperation("set value 操作")
@ResponseBody
@RequestMapping(value = "/set", method = RequestMethod.POST)
public String setVal(String key, String value) {
redisTemplate.opsForValue().set(key, value);
return "success set val";
}
@ApiOperation("get 操作")
@ResponseBody
@RequestMapping(value = "/get", method = RequestMethod.GET)
public String getValue(String key) {
String result = (String) redisTemplate.opsForValue().get(key);
System.err.println("======> 返回結(jié)果result:" + result);
return result;
}
}5.訪問與測試:http://localhost:8080/doc.html

到此這篇關(guān)于knife4j 整合 springboot的文章就介紹到這了,更多相關(guān)knife4j 整合 springboot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot3集成Knife4j的步驟以及使用(最完整版)
- SpringBoot?Knife4j框架&Knife4j的顯示內(nèi)容的配置方式
- SpringBoot與knife4j的整合使用過程
- springboot讀取bootstrap配置及knife4j版本兼容性問題及解決
- springboot3整合knife4j詳細(xì)圖文教程(swagger增強(qiáng))
- springboot整合knife4j全過程
- SpringBoot中使用Knife4J的解決方案
- springboot集成swagger3與knife4j的詳細(xì)代碼
- Springboot中整合knife4j接口文檔的過程詳解
- knife4j+springboot3.4異常無法正確展示文檔
相關(guān)文章
詳解 Corba開發(fā)之Java實(shí)現(xiàn)Service與Client
這篇文章主要介紹了詳解 Corba開發(fā)之Java實(shí)現(xiàn)Service與Client的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10
Java中隊(duì)列(Queue)和列表(List)的區(qū)別解析
Java中的列表(List)和隊(duì)列(Queue)是兩種常用的數(shù)據(jù)結(jié)構(gòu),它們分別用于不同的場景,列表是有序的,支持隨機(jī)訪問,允許重復(fù)元素,并且可以通過索引插入或刪除元素,下面通過本文給大家介紹Java中隊(duì)列(Queue)和列表(List)的區(qū)別,感興趣的朋友一起看看吧2025-03-03
Logback與Log4j2日志框架性能對比與調(diào)優(yōu)方式
這篇文章主要介紹了Logback與Log4j2日志框架性能對比與調(diào)優(yōu)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Java中 synchronized 和 volatile的核心區(qū)別解析
本文給大家介紹Java中synchronized和volatile的核心區(qū)別,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-09-09

