SpringBoot2.x整合swagger2/knife4j
阿新 • • 發佈:2021-08-23
knife4j就swagger的升級版API文件的一個框架,但是用起來比swagger方便多了,UI更加豐富。
基礎環境搭建可參考 swagger配置:swagger2搭建
實現步驟:
Step1: 匯入maven依賴
<!-- Swagger --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
Step2: knife4j配置類基本與swagger配置類一致
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; // 訪問地址localhost:8080/swagger-ui.html @Configuration @EnableKnife4j @EnableSwagger2WebMvc public class Swagger2Config { @Value("${swagger.enable}") private Boolean enable; @Bean public Docket webApiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("CRM-Api") .apiInfo(webApiInfo()) .enable(enable) // 是否顯示 .select() .build(); } private ApiInfo webApiInfo(){ return new ApiInfoBuilder() .title("CRM客戶管理系統-Api") .description("CRM-Api") .version("1.0") .contact(new Contact("robin", "", "")) .build(); } }
Step3: .yml配置
# swagger開啟 swagger: enable: true # 是否停用Knife4j文件 knife4j: production: false
Step4: 訪問根目錄/doc.html
http://ip:port/doc.html
參考自:https://blog.csdn.net/weixin_44739349/article/details/105839318