所有 API 路径前缀统一配置在 application.yml 中:
server:
port: 8182
servlet:
context-path: /apicontext-path: /api- 所有 Controller 自动添加此前缀
- 每个 Controller 使用相对路径,如
@RequestMapping("/chat") - 不要在 Controller 上写
/api前缀
context-path + Controller路径 = 完整路径
/api + /chat = /api/chat
/api + /investment = /api/investment
/api + /skills = /api/skills
/api + /rag = /api/rag
...
| Controller | RequestMapping | 完整路径 |
|---|---|---|
| ChatController | /chat |
/api/chat |
| FluxStreamController | /chat/flux |
/api/chat/flux |
| SseEmitterController | /chat/emitter |
/api/chat/emitter |
| SseStreamController | /chat/sse |
/api/chat/sse |
| GraphController | /graph |
/api/graph |
| LangfuseTestController | /observability |
/api/observability |
| RagController | /rag |
/api/rag |
| SkillsAgentController | /skills |
/api/skills |
| ToolChatController | /tools |
/api/tools |
| InvestmentDecisionController | /investment |
/api/investment |
@RestController
@RequestMapping("/your-module") // ✅ 正确:使用相对路径
public class YourController {
// 实际路径: /api/your-module
}
@RestController
@RequestMapping("/api/your-module") // ❌ 错误:不要重复添加 /api
public class YourController {
// 实际路径: /api/api/your-module (重复了!)
}// vite.config.ts
proxy: {
'/engine': {
target: 'http://localhost:8182',
rewrite: (path) => path.replace(/^\/engine/, '/api'),
},
}
// API 调用
fetch('/engine/investment/decide') // → http://localhost:8182/api/investment/decide// 直接调用完整路径
fetch('http://localhost:8182/api/investment/decide')只需修改 application.yml 中的 context-path:
# 示例 1: 使用 /v1 前缀
server:
servlet:
context-path: /v1
# 示例 2: 使用 /rest 前缀
server:
servlet:
context-path: /rest
# 示例 3: 移除前缀(直接在根路径)
server:
servlet:
context-path: /所有 Controller 的路径会自动适配,无需修改代码。
✅ 统一配置 - API 前缀集中管理,一处修改全局生效
✅ 避免重复 - Controller 只需定义相对路径
✅ 灵活切换 - 可随时通过配置切换 API 前缀
✅ 易于维护 - 清晰的职责分离,配置与代码分离