SpringBoot3运行访问knife4j网址:localhost:7000/doc.html前端页面提示:Failed to load resource: the server responded with a status of 404 ()
时间: 2025-02-25 19:46:28 浏览: 77
当你尝试通过 `http://localhost:7000/doc.html` 访问 Knife4j 的文档页面时遇到 404 错误,这通常表示请求的资源未找到。以下是几种常见的原因及解决办法:
### 可能的原因分析
1. **端口号错误**
确认你的 Spring Boot 应用确实是在 7000 端口上运行的。你可以检查应用启动日志,看看是否包含类似下面的日志信息:
```
Tomcat started on port(s): 7000 (http) with context path ''
```
2. **Knife4j 配置缺失**
- 检查项目中是否有引入依赖项,在 pom.xml 或 build.gradle 中确认是否存在对 knife4j 和 swagger-ui 相关依赖。
Maven 示例配置如下:
```xml
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
<!-- Swagger核心包 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
```
3. **Swagger/Swagger-UI启用设置问题**
如果你是使用SpringDoc OpenAPI替代了原生的Swagger,则应该访问 `/swagger-ui/index.html#` 路径而非 `doc.html`, 对应地需要调整路径。
同样也请确保已经启用了对应的Bean定义:
```java
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
// ...省略一些内容...
}
}
```
4. **安全限制导致无法加载静态资源文件**
若应用程序添加了 spring security 组件,默认可能会拦截所有 URL 请求包括 doc.html 页面,因此你需要排除某些URL模式不受保护,例如:
```java
http.authorizeRequests().antMatchers("/doc.html", "/webjars/**").permitAll();
```
5. **浏览器缓存影响**
清除浏览器缓存并刷新网页试试看;另外可以考虑换一个浏览器测试下是不是同样的结果。
6. **版本兼容性问题**
特别是如果你是从较旧版迁移到新版的时候,需要注意可能存在插件间的适配性差异,查阅官方更新说明来做出相应修改。
---
经过上述排查之后如果仍然存在疑问的话建议查看控制台具体的异常堆栈跟踪信息以及网络请求返回的具体报文,进一步定位根本原因。
阅读全文
相关推荐



















