0

Controller returns Java.lang.stackoverflow error when calling from ajax.

My Ajax function is like this.

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

Controller

@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
    }
}

Issue:

This is working fine when a form is submit then I can use all data by running foreach loop in jsp but when I tried to return list in my function then ajax work successfully and it also return me response

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

but when I tried to print list value in controller it's only printing one value.

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

Note:

If I comment obj line then everything works fine for me. e.g:

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

But I need to use this to get data as It contain data for different table.

Please find this file for error log that I'm getting in console.

So, What I'm doing wrong which cause this issue or I need to use any other way to prevent this issue.

Any suggestion or link will be helpful.

Luffy
  • 1,317
  • 1
  • 19
  • 41
  • Any error stack in the java server console or logs ? – Magus Oct 07 '15 at 12:07
  • Actually there are messages realated to java.lang.stackoverflow but they are two big so before I can see them they are replaced.. – Luffy Oct 07 '15 at 12:10
  • 1
    If you are logging to console, try increasing the buffersize of the console, or log into a file. – hotzst Oct 07 '15 at 12:12
  • ok give me a minute I'll try to add error message here – Luffy Oct 07 '15 at 12:13
  • @Magus I uploaded error log file Please check the question again – Luffy Oct 07 '15 at 12:25
  • 1
    It is possible that you have problem because Jackson JSON serializer is receiving whole object graph, and is serializing bidirectional mappinngs: school->student->owner->... (can't see your entities). Please see http://stackoverflow.com/questions/3340485/how-to-solve-circular-reference-in-json-serializer-caused-by-hibernate-bidirecti how to configure Jackson to handle parent/child linkage. – Magic Wand Oct 07 '15 at 13:00
  • @MagicWand Yes There is an issue of Jackson JSON serializer. Because of your comment I'm able to figured out the issue but still not able to resolved it. Any other link which can help me – Luffy Oct 08 '15 at 06:17

1 Answers1

0

According to your error log file, you have a cyclic recursion in your Java classes. So Jackson try to serialize some objects infinitely.

The main problem is here : com.school.model.Company_$$_javassist_8["users"]->org.hibernate.collection.PersistentSet[0]->com.school.model.User["company"]->com.school.model.Company_$$_javassist_8["users"]

You have a property users containing a set of User containing a property ompany containr a Company containing the same users. So infinity loop.

Magus
  • 14,796
  • 3
  • 36
  • 51
  • Thanks for your reply. But the same code is working fine when I put values in map and try to get them on some other page... – Luffy Oct 08 '15 at 04:27