gpt4 book ai didi

java - SpringBoot无法基于JSON创建类的实例

转载 作者:行者123 更新时间:2023-12-01 17:30:39 25 4
gpt4 key购买 nike

我在 Spring 启动时遇到问题。这是 Controller

public void addNewWatcher (@RequestBody WatcherInput input) {
boolean isDeptIDExist = false;
boolean isCourseIDExist = false;
for(Department department : manager){
if(input.getDeptId() == department.getDepartmentID()){
isDeptIDExist = true;
for(Course course : department.getCourses()){
if(input.getCourseId() == course.getCourseID()){
isCourseIDExist = true;
break;
}
}
}
}
if(!isDeptIDExist || !isCourseIDExist){
throw new RESTFileNotFound("deptID or courseID do not exist");
}

try {
Course course = manager.get(input.getDeptId()).getCourse(input.getCourseId());
ApiCourseWrapper selectedCourse = new ApiCourseWrapper(input.getCourseId(), course.getCatalog());
ApiDepartmentWrapper selectedDepartment = new ApiDepartmentWrapper(input.getDeptId(), course.getSubject());

ApiWatcherWrapper newWatcher = new ApiWatcherWrapper(nextWatcherID.incrementAndGet(), selectedDepartment, selectedCourse);
watchers.add(newWatcher);

} catch (Exception e) {
e.printStackTrace();
}

}

观察者输入

public class WatcherInput {
private int deptId = 0;
private int courseId = 0;

public WatcherInput() {
}

public WatcherInput(int deptId, int courseId) {
this.deptId = deptId;
this.courseId = courseId;
}

public int getDeptId() {
return (int)deptId;
}

public int getCourseId() {
return (int)courseId;
}
}

问题是“JSON解析错误:无法构造API.WrapperClass.WatcherInput的实例(尽管至少存在一个创建者):没有用于从Number反序列化的int/Int参数构造函数/工厂方法值 (11);嵌套异常为 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造 API.WrapperClass.WatcherInput 的实例(尽管至少存在一个 Creator):无 int/Int-参数构造函数/工厂方法从数字值 (11)\n 反序列化 [源:(PushbackInputStream);行:1,列:1]",

我认为SpringBoot无法创建WatcherInput的实例,但我不知道为什么

最佳答案

您可以尝试改为使用 int 类型 Integer 吗?反序列化其中一个数字时似乎遇到问题。

关于java - SpringBoot无法基于JSON创建类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61128657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com