swagger3 + knife4j實現(xiàn)優(yōu)雅接口文檔

swagger大家都不陌生,強大的接口文檔工具,最近出了swaager3版本,馬上嘗鮮

knife4j功能強大,頁面更美觀,在線調(diào)試功能很喜歡,不用再在postman手動輸鏈接調(diào)試了


全局參數(shù)添加header

支持離線文檔下載


添加依賴?implementation"io.springfox:springfox-boot-starter:3.0.0? ?習(xí)慣用gradle

implementation"com.github.xiaoymin:knife4j-spring-boot-starter:3.0.1


新建配置文件SwaggerConfiguration

/**

* 基于Swagger生成API文檔

*

* @author qiuw

* *@EnableOpenApi:啟動OpenApi的類; 之前是@EnableSwagger2

*/

@Configuration

@EnableOpenApi

@EnableKnife4j

@Import(BeanValidatorPluginsConfiguration.class)

public class SwaggerConfiguration {

@Bean

? ? public DocketcreateRestApi() {

return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select()

.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

.paths(PathSelectors.any())

.build();

? ? }

private ApiInfoapiInfo() {

return new ApiInfoBuilder()

.title("標(biāo)題")

.description("描述")

.contact(new Contact("聯(lián)系人", "www.xxx.com", "xxx@xxx.com"))

.version("1.0.0")

.build();

? ? }

}


就這么簡單,然后就是接口文檔編寫和之前的一樣


@Api??@ApiOperation? 等等就不再說了


原生swaager頁面訪問?http://localhost:8080//swagger-ui/index.html? 端口那些自己改

knife4j 訪問?http://localhost:8080/doc.html


給knife4j設(shè)置權(quán)限

在yml或properties配置文件加配置

# 接口文檔配置

knife4j:

enable:true? # 開啟權(quán)限驗證

? basic:

enable:true

? ? username: eas# 登錄賬號

? ? password: eas# 登錄密碼

就實現(xiàn)攔截了,官方文檔這塊還沒更新,自己試出來的knife4j官網(wǎng)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容