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
版本兼容慣關系:
springboot 1.5.x~2.0.0 對應 <Knife4j 2.0.0
springboot 2.0 ~ 2.2 對應 Knife4j 2.0.0 ~ 2.0.6
springboot 2.2.x~2.4.0 對應 Knife4j 2.0.6 ~ 2.0.9
springboot 2.4.0~2.7.x 對應 >=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("服務條款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:關閉swagger,false:開啟swagger # true - 設置未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("======> 返回結果result:" + result);
return result;
}
}5.訪問與測試:http://localhost:8080/doc.html

到此這篇關于knife4j 整合 springboot的文章就介紹到這了,更多相關knife4j 整合 springboot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Springboot3集成Knife4j的步驟以及使用(最完整版)
- SpringBoot?Knife4j框架&Knife4j的顯示內(nèi)容的配置方式
- SpringBoot與knife4j的整合使用過程
- springboot讀取bootstrap配置及knife4j版本兼容性問題及解決
- springboot3整合knife4j詳細圖文教程(swagger增強)
- springboot整合knife4j全過程
- SpringBoot中使用Knife4J的解決方案
- springboot集成swagger3與knife4j的詳細代碼
- Springboot中整合knife4j接口文檔的過程詳解
- knife4j+springboot3.4異常無法正確展示文檔
相關文章
5分鐘快速學會spring boot整合JdbcTemplate的方法
這篇文章主要給大家介紹了如何通過5分鐘快速學會spring boot整合JdbcTemplate的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring boot整合JdbcTemplate具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12
Spring?Security實現(xiàn)接口放通的方法詳解
在用Spring?Security項目開發(fā)中,有時候需要放通某一個接口時,我們需要在配置中把接口地址配置上,這樣做有時候顯得麻煩。本文將通過一個注解的方式快速實現(xiàn)接口放通,感興趣的可以了解一下2022-05-05
Java 自定義Spring框架與Spring IoC相關接口分析
Spring框架是由于軟件開發(fā)的復雜性而創(chuàng)建的。Spring使用的是基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅僅限于服務器端的開發(fā)2021-10-10

