在 CentOS 上运行 Swagger(如 Swagger UI / Springfox / swagger-ui / FastAPI 等) 报错,原因很多,需要看具体报错信息才能精准定位。下面我按常见场景 + 排查思路给你一个通用解决清单,你对照看看是哪一种。
请把完整报错信息贴出来,比如:
404 Not FoundFailed to load API definitionCaused by: java.lang.NoClassDefFoundErrorConnection refusedPermission deniedCentOS 默认有 firewalld
# 查看防火墙
systemctl status firewalld
# 开放端口(如 8080)
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
如果是云服务器,还要去安全组放行端口。
常见于:
✅ 正确访问地址:
http://IP:端口/swagger-ui.html
或
http://IP:端口/swagger-ui/index.html
✅ 确认依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
✅ Spring Boot 2.6+ 需要加:
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
常见报错:
org.springframework.context.ApplicationContextException
✅ 推荐组合:
Springdoc:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.x</version>
</dependency>
访问:
/swagger-ui/index.html
getenforce
如果是 Enforcing:
setenforce 0
(生产环境不建议长期关闭)
检查:
java -version
node -v
如果是:
升级对应运行环境。
curl http://localhost:8080/v2/api-docs
如果返回 JSON → 后端 OK
如果连不上 → 后端没起 or 配置错误
请告诉我:
cat /etc/centos-release)我可以直接给你针对性解决方案。