在 Linux 下调试 Swagger(通常指 Swagger UI / OpenAPI),一般取决于你用的技术栈。下面按最常见场景给你一套「通用 + 实战」的调试思路。
先确认你属于哪一种:
| 场景 | 说明 |
|---|---|
| ✅ Swagger UI 打不开 | 页面 404 / 空白 |
| ✅ Swagger 接口返回 404 / 500 | API 文档接口异常 |
| ✅ Swagger 参数不显示 | 注解或配置问题 |
| ✅ Swagger 请求失败 | CORS / 鉴权 / 代理 |
| ✅ Swagger 不更新 | 缓存或配置未生效 |
Swagger UI 本质只是渲染一个 JSON:
# 常见地址
curl http://localhost:8080/v2/api-docs
curl http://localhost:8080/v3/api-docs
✅ 正常:返回 JSON
❌ 异常:接口本身有问题(不是 UI 问题)
在 Linux 下用 Chrome / Edge / Firefox:
F12常见错误:
Failed to load API definitionCORS error401 UnauthorizedSwagger 本质上由后端生成:
# Spring Boot
tail -f logs/app.log
# Node.js
pm2 logs
重点看:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.x.x</version>
</dependency>
http://localhost:8080/swagger-ui.html
http://localhost:8080/swagger-ui/index.html
✅ 检查:
@Configuration
public class SwaggerConfig {
}
✅ 是否被拦截:
spring.mvc.static-path-pattern=/**
✅ Spring Security 放行:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll();
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
http://localhost:3000/api-docs
curl http://localhost:3000/swagger.json
jsonlint 检查)docker ps
✅ 确认:
-p 8080:8080
docker exec -it 容器名 /bin/bash
curl localhost:8080/v3/api-docs
# 查看端口
ss -lntp | grep 8080
# 查看进程
ps -ef | grep java
# 查看日志
tail -f app.log
# 测试接口
curl -v http://localhost:8080/v3/api-docs
/v2/api-docs 或 /v3/api-docs 返回 JSON你可以直接贴:
我可以一步一步帮你把问题定位到具体配置或代码。