在 Linux 下 Swagger(通常指 Swagger UI / swagger-editor / Springfox、springdoc-openapi 等)访问或加载很慢,通常不是单一原因,而是网络、DNS、JVM、JSON 生成、前端资源等因素叠加。下面按最常见 → 较少见总结原因和对应解决方案。
很多 Swagger UI 页面会加载:
cdn.jsdelivr.netunpkg.comswagger.ioLinux 服务器访问国外 CDN 很慢或被墙
✅ 表现:
swagger-ui-bundle.js 等卡住curl 超时✅ 解决方案:
# 使用本地 swagger-ui
git clone https://github.com/swagger-api/swagger-ui.git
或 Spring Boot 项目改用本地 Swagger UI
Swagger 慢 ≠ 页面慢,而是 接口扫描慢
✅ 常见原因:
Springfox(已停止维护)✅ 解决方案: ✅ 强烈推荐:迁移到 springdoc-openapi
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
</dependency>
✅ 禁用不必要扫描:
springdoc:
api-docs:
enabled: true
packages-to-scan: com.xxx.controller
Java / Swagger 启动时可能进行 DNS 反查
✅ 表现:
Initializing Swagger✅ 检查:
nslookup localhost
✅ 解决:
# /etc/hosts
127.0.0.1 localhost
访问:
/v2/api-docs
/v3/api-docs
✅ 如果:
✅ 优化:
springdoc:
default-flat-param-object: true
model-and-view-allowed: false
Java 在 Linux 下生成随机数慢
✅ 表现:
✅ 解决(强烈推荐):
yum install haveged -y
systemctl enable haveged
systemctl start haveged
或 JVM 参数:
-Djava.security.egd=file:/dev/./urandom
SELinux 可能导致:
✅ 排查:
getenforce
✅ 临时关闭测试:
setenforce 0
✅ 解决:
springdoc.group-configscurl -w "@curl-format.txt" http://localhost:8080/v3/api-docs
浏览器 → Network → 看哪个 JS 最慢
tail -f logs/app.log
✅ Linux + Spring Boot 推荐组合
你可以补充:
我可以直接给你针对你环境的优化方案。