Spring中的Ajax返回java.lang.stackoverflow错误

时间:2015-10-07 12:03:54

标签: javascript java jquery ajax spring

从ajax调用时,Controller返回Java.lang.stackoverflow错误。

我的Ajax功能是这样的。

$.ajax({
    url: '${pageContext.servletContext.contextPath}/exam/test',
    type: 'POST',
    data: 'examName='+examName,
    success: function(response) {
        alert(response);
    }
});

控制器

@RequestMapping(value = "/exam/test", method = RequestMethod.POST)
public @ResponseBody List<SchoolExam> examsTest(@ModelAttribute(value = "examName") String examName, BindingResult result, WebRequest webRequest, ModelMap map, Principal principal) {

    User loggedUser = userService.getUserByUserName(principal.getName());

    ***********************
        Some code here
    ***********************

    List<SchoolExam> schoolExams = new ArrayList<SchoolExam>();

    for (School school : schools) {
        if (student) {
            Set<Student> students = school.getStudents();
            for(Student std : students) {
                if (std != null && !std.isEmpty()) {
                    schoolExams.add(new SchoolExam(std, true));
                }
            }
        }
        if (teacher) {
            Set<Teacher> teachers = school.getEvents();
            for (Teacher tchr : teachers) {
                if (loggedUser.equals(tchr.getOwner())) {
                    schoolExams.add(new SchoolExam(tchr, true));
                }
            }
        }
        if (exam) {
            Set<Exam> exams = school.getCampaigns();
            for (Exam exam1 : exams) {
                if (loggedUser.equals(exam1.getOwner())) {
                    schoolExams.add(new SchoolExam(exam1, true));
                }
            }
        }
    }
    return schoolExams;
}

SchoolExam

public SchoolExam(Object obj, boolean editable) {
    this.editable = editable;
    if (obj instanceof Student) {
        Student student = (Student) obj;
        this.id = student.getId();
        this.name = student.getName();
        this.type = Constants.Student;
        this.obj = student; // <-- This is causing issue here
    }
    if (obj instanceof Teacher) {
        Teacher teacher = (Teacher) obj;
        this.id = teacher.getId();
        this.name = teacher.getName();
        this.type = Constants.Teacher;
        this.obj = teacher; // <-- This is causing issue here
    }
    if (obj instanceof Exam) {
        Exam exam = (Exam) obj;
        this.id = exam.getId();
        this.name = exam.getName();
        this.type = Constants.Exam;
        this.obj = exam; // <-- This is causing issue here
    }
}

问题:

当表单提交时,这工作正常然后我可以通过在jsp中运行foreach循环来使用所有数据但是当我尝试在我的函数中返回列表然后ajax成功工作并且它也返回我的响应

response in ajax

[
    {
        "id":"2123244",
        "name":"UK School",
        "type":"exam",
        "editable":true,
        "obj":
        {
            "id":"2123244",
            "authorizationRequired":false,
            "owner":
            {
                "id":"5676764554",
                "company":
                {
                    "id":"55435435345",
                    "name":"SchoolTest Software",
                    "enabled":true,
                    "size":3,
                    "sector":null,
                    "phone":"1231231232",
                    "schoolFees":5000,
                    "location":"US",
                    "users":
                            [
                                {
                                    "id":"5676764554",
                                    "company": // <-- Start Repeating here
                                            {
                                                "id":"55435435345",
                                                "name":"SchoolTest Software",
                                                "enabled":true,
                                                "size":3,
                                                "sector":null,
                                                "phone":"1231231232",
                                                "schoolFees":5000,
                                                "location":"US",
                                                "users":
                                                        [
                                                            {
                                                                "id":"5676764554",
                                                                "company": // <-- Repeating again
                                                                        {
                                                                        "id":"55435435345",
                                                                        "name":"SchoolTest Software",
                                                                        "enabled":true,
                                                                        "size":3,
                                                                        "sector":null,
                                                                        "phone":"1231231232",
                                                                        "schoolFees":5000,
                                                                        "location":"US",
                                                                        "users":
                                                                                [
                                                                                    {
                                                                                        "id":"5676764554",
                                                                                        "company":// <-- It keeps repeating it self

但是当我尝试在控制器中打印列表值时,它只打印一个值。

e.g:

for (SchoolExam schoolExam : schoolExams) {
    System.out.println("Name: " + schoolExam.getName());
    System.out.println("ID: " + schoolExam.getId());
    Exam exam = (Exam) schoolExam.getObj();
    System.out.println("Exam Name: " + exam.getName());
}

Output:

Name: UK School
ID: 2123244
Exam Name: UK School

注意:

如果我评论obj线,那么一切都适合我。 e.g:

this.obj = student; 
this.obj = teacher; 
this.obj = exam; 

但我需要使用它来获取数据,因为它包含不同表的数据。

请查看this file以获取我在控制台中收到的错误日志。

所以,我做错了导致这个问题,或者我需要用其他任何方法来防止这个问题。

任何建议或链接都​​会有所帮助。

1 个答案:

答案 0 :(得分:0)

根据您的错误日志文件,您的Java类中有一个循环递归。所以杰克逊试图无限地序列化一些物体。

主要问题在于:com.school.model.Company_$$_javassist_8["users"]->org.hibernate.collection.PersistentSet[0]->com.school.model.User["company"]->com.school.model.Company_$$_javassist_8["users"]

您有一个属性users,其中包含一组User,其中包含属性ompany,其中包含Company users个{{1}}。无限循环。