环境: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.htmltags-sorter: alphaoperations-sorter: alphaenabled: trueapi-docs:path: /v3/api-docsenabled: truegroup-configs:group: platformpaths-to-match: /**packages-to-scan: com.license4j.licenseknife4j:enable: truesetting:language: zh_cn
3.代码配置
addInterceptors主要放开了文档的路径访问
addResourceHandlers主要设置了接口文档静态资源路径
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 {@Resourceprivate LicenseInterceptor licenseInterceptor;public WebMvcRegistrationsConfig(LicenseInterceptor licenseInterceptor) {this.licenseInterceptor = licenseInterceptor;}/*** 拦截器配置* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 无需拦截的接口集合List<String> ignorePath = new ArrayList<>();// swaggerignorePath.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);}/*** 静态资源配置* @param registry*/@Overridepublic 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基本配置
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");}@Beanpublic OpenAPI springShopOpenAPI() {return new OpenAPI().info(info()).externalDocs(externalDocumentation());}
}
4.访问路径 ip+端口+doc.html