我是新手,我尝试启动一个链接到 Optaplanner 的 Spring 应用程序,它将解决问题并将考试安排在时间表上。 我解决了缺少 jars 的各种问题,并在 main 上启动了该应用程序。 但是,它给出了错误:
Exception in thread "main" java.lang.ClassCastException: class org.springframework.boot.context.event.ApplicationStartingEvent cannot be cast to class org.springframework.boot.context.event.ApplicationPreparedEvent (org.springframework.boot.context.event.ApplicationStartingEvent and org.springframework.boot.context.event.ApplicationPreparedEvent are in unnamed module of loader 'app')
这是 TimeTable 类:
package models;
import java.util.List;
import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.PlanningScore;
import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.solution.ProblemFactCollectionProperty;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;
@PlanningSolution
public class Timetable {
@ValueRangeProvider(id = "PeriodeRange")
@ProblemFactCollectionProperty
public List<Periode> periodeList;
@ValueRangeProvider(id = "SalleRange")
@ProblemFactCollectionProperty
public List<Salle> salleList;
@PlanningEntityCollectionProperty
public List<Examen> examenList;
@PlanningScore
public HardSoftScore score;
public void TimeTable(List<Periode> periodeList, List<Salle> roomList,
List<Examen> examenList) {
this.periodeList = periodeList;
this.salleList = roomList;
this.examenList = examenList;
}
public List<Periode> getperiodeList() {
return periodeList;
}
public List<Salle> getsalleList() {
return salleList;
}
public List<Examen> getexamenList() {
return examenList;
}
public HardSoftScore getScore() {
return score;
}
}
定义求解器的类:
package models;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import modeles.Timetable;
import org.optaplanner.core.api.solver.SolverJob;
import org.optaplanner.core.api.solver.SolverManager;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
@RequestMapping("/Timetable")
public class TimetableController {
@Autowired
public SolverManager<Timetable, UUID> solverManager;
@PostMapping("/solve")
public Timetable solve(@RequestBody Timetable problem) {
UUID problemId = UUID.randomUUID();
// Submit the problem to start solving
SolverJob<Timetable, UUID> solverJob = solverManager.solve(problemId, problem);
Timetable solution;
try {
// Wait until the solving ends
solution = solverJob.getFinalBestSolution();
} catch (InterruptedException | ExecutionException e) {
throw new IllegalStateException("Solving failed.", e);
}
return solution;
}
}
这里是主要的:
package models;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TimeTableSpringBootApp {
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(TimeTableSpringBootApp.class, args);
}
}
任何答案将不胜感激。 提前谢谢你。
最佳答案
我怀疑您的项目的依赖树以某种方式在其类路径中有两次 spring 类 ApplicationStartingEvent
(来自不同的 jar),这会导致类转换异常。
尝试在您的项目和 optaplanner spring boot 学校时间表快速启动上运行 mvn dependency:tree
。我怀疑您在依赖树中混合了 spring 版本。
关于Spring:ApplicationStartingEvent 不能转换为 ApplicationPreparedEvent for OptaPlanner Examination App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65531904/