SpringBoot整合Knife4j實現(xiàn)接口文檔自動生成的最佳實踐
接口文檔是前后端協(xié)作的橋梁。手寫文檔費時且容易和代碼不一致,Knife4j 可以自動從注解生成接口文檔,提供在線調(diào)試功能。
一、為什么選 Knife4j
| 對比 | Knife4j | Swagger UI | 手寫文檔 |
|---|---|---|---|
| UI 美觀度 | ????? 中文友好 | ??? 英文原生 | ??? |
| 在線調(diào)試 | ? 支持 | ? 支持 | ? |
| 代碼侵入 | 低(注解) | 低(注解) | 無(但費時) |
| 維護成本 | 自動同步 | 自動同步 | ??? 極高 |
二、集成 Knife4j
1. 引入依賴
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>2. 配置
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
knife4j:
enable: true
setting:
language: zh-CN
enable-swagger-models: true
enable-document-manage: true
swagger-model-order: 13. 配置類
@Configuration
@EnableKnife4j
public class Knife4jConfig {
@Bean
public Docket defaultApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zhang"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("秒殺系統(tǒng)接口文檔")
.description("電商秒殺系統(tǒng) RESTful API")
.version("V1.0")
.contact(new Contact("張政", "", ""))
.build();
}
}
訪問 http://localhost:9090/doc.html 即可看到接口文檔頁面。
到此這篇關(guān)于SpringBoot整合Knife4j實現(xiàn)接口文檔自動生成的最佳實踐的文章就介紹到這了,更多相關(guān)SpringBoot Knife4j接口文檔自動生成內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot?Webflux創(chuàng)建TCP/UDP?server并使用handler解析數(shù)據(jù)
這篇文章主要介紹了SpringBoot?Webflux創(chuàng)建TCP/UDP?server并使用handler解析數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringBoot集成ElasticSearch的示例代碼
Elasticsearch是用Java語言開發(fā)的,并作為Apache許可條款下的開放源碼發(fā)布,是一種流行的企業(yè)級搜索引擎,本文給大家介紹SpringBoot集成ElasticSearch的示例代碼,感興趣的朋友一起看看吧2022-02-02
解決HttpPost+json請求---服務(wù)器中文亂碼及其他問題
這篇文章主要介紹了解決HttpPost+json請求---服務(wù)器中文亂碼及其他問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
配置JAVA環(huán)境變量中CLASSPATH變量的作用
這篇文章主要介紹了配置JAVA環(huán)境變量中CLASSPATH變量的作用,需要的朋友可以參考下2023-06-06

