问题描述
在SpringBoot3项目中,集成knife4j,pom.xml 如下:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
访问http://localhost:8080/doc.html 报错:No endpoint GET /doc.html
解决办法
在实现WebMvcConfigurer的配置文件中,设置静态资源映射,Java代码如下:
@Configuration
@ConditionalOnProperty(name = "springdoc.enabled", havingValue = "true", matchIfMissing = true)
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(new Info()
.title("XX API接口文档")
.version("1.0")
.description("XX API接口文档")
.license(new License()
.name("Apache 2.0")
.url("https://www.apache.org/licenses/LICENSE-2.0.html")));
}
/**
* 设置静态资源映射
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
文章评论