3

代码给出错误

java.lang.IllegalStateException:参数 [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?

4

1 回答 1

0

在第一种情况下,杰克逊无法反序列化您的响应。我建议将您的返回类型更改为Map<String, ? extends object> 让我们知道问题是否仍然存在

于 2012-11-11T08:31:38.247 回答