如何将Timefold排课快速启动Demo切换为自定义JSON数据源?
切换Timefold学校排课项目数据源:从Demo Resource到自定义JSON输入
要替换默认的Demo示例数据,必须修改源码,必要时调整前端页面,具体步骤如下:
1. 停用默认的Demo资源类
找到项目中的TimeTableDemoResource类,通过以下方式禁用它:
- 注释掉类上的
@RestController注解,让Spring Boot不再扫描并加载这个接口; - 或者直接将该类移至项目的非组件扫描目录,避免自动注册。
这样项目启动后就不会默认加载Demo的示例数据了。
2. 新增自定义数据接收接口
创建一个新的REST控制器类(比如CustomTimeTableResource),用来接收手动输入的JSON数据并触发排课计算:
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import ai.timefold.solver.core.api.solver.SolverManager; @RestController @RequestMapping("/timeTable/custom") public class CustomTimeTableResource { private final SolverManager<TimeTable, Long> solverManager; public CustomTimeTableResource(SolverManager<TimeTable, Long> solverManager) { this.solverManager = solverManager; } @PostMapping("/solve") public TimeTable solveCustom(@RequestBody TimeTable timeTable) { // 使用自定义传入的TimeTable数据执行排课 return solverManager.solveAndFetchLatest(1L, timeTable); } }
确保你的JSON数据结构与TimeTable实体类的字段完全匹配(需包含lessons、rooms、teachers等核心列表)。
3. 调整前端页面(可选,若需在UI手动输入)
如果要在项目自带的前端页面输入JSON,找到src/main/resources/static下的HTML文件,修改原有的请求逻辑:
- 将调用
/timeTable/demo/solve的AJAX请求地址,替换为新的/timeTable/custom/solve; - 添加一个文本区域(
<textarea>)让用户输入JSON数据,发送请求时将文本内容作为请求体传递。
4. 测试自定义数据排课
- 用Maven启动项目:
mvn spring-boot:run - 通过Postman或修改后的前端页面,发送符合格式的JSON数据到
/timeTable/custom/solve接口,即可得到基于自定义数据的排课结果。
内容的提问来源于stack exchange,提问作者EdoZp12
相关产品推荐
相关产品推荐

