環(huán)境:JDK17,Springboot3+,springdoc2+,knife4j 4+
Springdoc本身也是集成了Swagger3,而knife4j美化了Swagger3的UI
Knife4j官網:
快速開始 | Knife4j
Springdoc官網
OpenAPI 3 Library for spring-boot
1.pom配置
由于此knife4j內依賴了SpringDoc,因此不用另外引入springdoc的依賴
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>
springdoc依賴(無需引入),親測引入也不會沖突
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>
2.application.yml配置
springdoc:
swagger-ui:
path: /swagger-ui.html
tags-sorter: alpha
operations-sorter: alpha
enabled: true
api-docs:
path: /v3/api-docs
enabled: true
group-configs:
group: platform
paths-to-match: /**
packages-to-scan: com.license4j.license
knife4j:
enable: true
setting:
language: zh_cn
3.代碼配置
addInterceptors主要放開了文檔的路徑訪問
addResourceHandlers主要設置了接口文檔靜態(tài)資源路徑
addResourceHandlers配置很重要,不配置會導致接口文檔404,后臺也會報異常
No mapping for GET /doc.html
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class WebMvcRegistrationsConfig extends WebMvcConfigurationSupport {
@Resource
private LicenseInterceptor licenseInterceptor;
public WebMvcRegistrationsConfig(LicenseInterceptor licenseInterceptor) {
this.licenseInterceptor = licenseInterceptor;
}
/**
* 攔截器配置
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 無需攔截的接口集合
List<String> ignorePath = new ArrayList<>();
// swagger
ignorePath.add("/swagger-resources/**");
ignorePath.add("/doc.html");
ignorePath.add("/v3/**");
ignorePath.add("/webjars/**");
ignorePath.add("/springdoc/**");
ignorePath.add("/static/**");
ignorePath.add("/templates/**");
ignorePath.add("/error");
ignorePath.add("/cipher/check");
ignorePath.add("/manager/login");
ignorePath.add("/swagger-ui.html");
//先攔截認證,再攔截授權
registry.addInterceptor(licenseInterceptor).addPathPatterns("/**").excludePathPatterns(ignorePath);
}
/**
* 靜態(tài)資源配置
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
swagger基本配置文章來源:http://www.zghlxwxcb.cn/news/detail-696643.html
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SwaggerConfig {
private Info info(){
return new Info()
.title("API接口文檔")
.description("")
.version("v1.0.0");
}
@Bean
public OpenAPI springShopOpenAPI() {
return new OpenAPI()
.info(info())
.externalDocs(externalDocumentation());
}
}
4.訪問路徑 ip+端口+doc.html文章來源地址http://www.zghlxwxcb.cn/news/detail-696643.html
到了這里,關于Springboot3.0.0+集成SpringDoc并配置knife4j的UI的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!