SpringBoot无法根据JSON创建类的实例。

huangapple 未分类评论41阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年4月10日 03:19:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/61128657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定