在 Debian 下测试 Swagger(OpenAPI) 接口,常见有 Swagger UI、curl、Postman、命令行工具 等方式。下面按最常用场景给你一个完整说明。
一般 Swagger 提供以下地址之一:
http://localhost:8080/swagger-ui.html
http://localhost:8080/swagger-ui/
http://localhost:8080/api-docs
http://localhost:8080/v3/api-docs
✅ 先确认接口是否可访问:
curl http://localhost:8080/v3/api-docs
如果有 JSON 返回,说明 Swagger 接口正常。
在 Debian 桌面或转发端口后访问:
http://localhost:8080/swagger-ui.html
或:
http://localhost:8080/swagger-ui/index.html
✅ 在页面中:
在本地电脑执行:
ssh -L 8080:localhost:8080 user@debian-server
然后本地浏览器访问:
http://localhost:8080/swagger-ui.html
curl http://localhost:8080/api/users
curl -X POST http://localhost:8080/api/user \
-H "Content-Type: application/json" \
-d '{"name":"Tom","age":18}'
curl http://localhost:8080/api/user/1 \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9..."
sudo apt install openapi-generator
openapi-generator generate \
-i http://localhost:8080/v3/api-docs \
-g python \
-o ./client
http://localhost:8080/v3/api-docs
✅ 适合复杂参数、Header、Auth 测试
检查:
Spring Boot 示例放行:
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll()
.anyRequest().authenticated()
);
return http.build();
}
说明接口需要认证:
| 场景 | 推荐方式 |
|---|---|
| 本地开发 | Swagger UI |
| 服务器 | curl + SSH 端口转发 |
| 自动化 | openapi-generator |
| 复杂测试 | Postman |
如果你愿意,可以告诉我:
我可以给你针对你项目的精确测试方案。