1. 程式人生 > 其它 >spring boot 2.6.2 整合 springfox3.0

spring boot 2.6.2 整合 springfox3.0

springfox3.0與spring boot 2.6.2整合之後,發現swagger3.0的簡直粗暴 無需任何配置,專案就能直接在spring boot 中啟動成功。主要得益於spring boot 的自動配置。在配置檔案中提供了自動開啟和相應的專案。其配置項是

spring.mvc.pathmatch.matching-strategy=ant_path_matcher // 此配置項是因為spring boot 2.6.2 不相容部分問題導致的
springfox.documentation.swagger-ui.enabled=false //spring boot提供的自動開啟或者關閉。 線上關閉 線下開啟

1. 新增專案依賴
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

2. 不需要在其controller main添加註解,啟動之後就可以看到預設的執行

訪問地址是: UI : http://localhost:8080/swagger-ui/index.html#/

doc :http://localhost:8080/v3/api-docs

3. 如果調整配置

package com.example.springfox.config;

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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
* @author: hett
* @date: 2022/1/17 10:13
*/
@Configuration
public class SwaggerConfig {

@Bean
Docket docket(){
//選擇版本3
return new Docket(DocumentationType.OAS_30)
//選擇生成的介面文件
.select()
// //包所在的路徑
.apis(RequestHandlerSelectors.basePackage("com.example.springfox.controller"))
//預設報下所有的介面都生成
.paths(PathSelectors.any())
.build()
//介面文件初始化
.apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.description("hett")
.contact(new Contact("hett",null,"[email protected]"))
.version("1.0")
.title("fox test")
.build();
}
}
//可以把配置檔案寫在一個屬性中
調整之後的結果