英文:
SpringBoot fail to create an Instance of class based on JSON
问题
以下是已翻译的内容:
我在使用Spring Boot时遇到问题。
这是控制器部分:
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();
}
}
WatcherInput类:
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 parse error: Cannot construct instance of 'API.WrapperClass.WatcherInput' (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (11); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of 'API.WrapperClass.WatcherInput' (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (11)\n at [Source: (PushbackInputStream); line: 1, column: 1]",
我认为Spring Boot无法创建WatcherInput的实例,但我不知道为什么。
英文:
I have problem with Spring boot.
Here is 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();
}
}
WatcherInput
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;
}
}
The problem is "JSON parse error: Cannot construct instance of API.WrapperClass.WatcherInput
(although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (11); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of API.WrapperClass.WatcherInput
(although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (11)\n at [Source: (PushbackInputStream); line: 1, column: 1]",
I think the SpringBoot cannot create an Instance of WatcherInput, but i dont know why
答案1
得分: 0
你能尝试将 int
类型改为 Integer
吗?似乎在反序列化数字时出现了问题。
英文:
Can you try making the int
types Integer
instead? It seems like it's having problems deserializing one of the numbers.
专注分享java语言的经验与见解,让所有开发者获益!
评论