springboot3集成knife4j访问doc.html报No endpoint GET /doc.html解决

2025-05-11 78点热度 0人点赞 0条评论

问题描述

在SpringBoot3项目中,集成knife4j,pom.xml 如下:

<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId> <version>4.4.0</version> </dependency>
<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/");
}
}
@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/"); } }
@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/");
    }
}

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论