spring mvc controller error java.lang.IllegalState

2019-07-20 09:16发布

Code gives an error

java.lang.IllegalStateException: No suitable resolver for argument [0][type=org.jopenclass.form.Course]

It sends a JSON response to an ajax call. I use hibernate to persist the objects.

    @RequestMapping(value = "/savecourse", method = RequestMethod.POST)
public @ResponseBody
Object saveLecturer(@Valid @ModelAttribute(value = "course") Course course,
        BindingResult result) {

    Map<String, Object> response = new HashMap<String, Object>();

    if (result.hasErrors()) {
        List<ObjectError> results = result.getAllErrors();
        for (ObjectError objectError : results) {
            System.out.println(objectError.getDefaultMessage());
        }
        response.put("message", "Could not add the Course to the system.");
    } else {
        try {
            course.setId(courseDao.saveCourse(course));//returns the id
            response.put("course", course);

        } catch (Exception e) {
            System.out.println(e);

        }
    }

    return response;
}

But when I create a new object and copy the parameters to the other object, it works fine. The second method(Not a good method of course) works well. All the parameters in the request object are set to the cse object as well.

    @RequestMapping(value = "/savecourse", method = RequestMethod.POST)
public @ResponseBody
Object saveLecturer(@Valid @ModelAttribute(value = "course") Course course,
        BindingResult result) {

    Map<String, Object> response = new HashMap<String, Object>();

    if (result.hasErrors()) {
        List<ObjectError> results = result.getAllErrors();
        for (ObjectError objectError : results) {
            System.out.println(objectError.getDefaultMessage());
        }
        response.put("message", "Could not add the Course to the system.");
    } else {
        try {
            course.setId(courseDao.saveCourse(course));//returns the id
            Course cse = new Course();
            cse.setId(course.getId());
            cse.setCourseName(course.getCourseName());
            cse.setFee(course.getFee());
            Lecturer lec = new Lecturer();
            lec.setId(course.getLecturer().getId());
            lec.setFirstName(course.getLecturer().getFirstName());
            lec.setLastName(course.getLecturer().getLastName());
            cse.setLecturer(lec);
            cse.setGrade(course.getGrade());
            response.put("course", cse);

        } catch (Exception e) {
            System.out.println(e);

        }
    }

    return response;
}

What is wrong in the first method?

1条回答
一夜七次
2楼-- · 2019-07-20 09:49

In the first case Jackson is not able to deserialize your response. I would suggest changing your return type to Map<String, ? extends object> Let us know if the problem persists

查看更多
登录 后发表回答