java.lang.StackOverflowError in spring controller

1.1k Views Asked by At

TreesController.java This is my controller Methodenter code here

    package com.reveal.web.controller;

    import java.util.List;

    import org.springframework.http.MediaType;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;

    import com.reveal.web.controller.EnsureCapacity.Technology;

    @Controller
    public class TreesController {

        @RequestMapping(value="/mytree",method = RequestMethod.GET)
        public String printWelcome(ModelMap model) {
            return "tree";
        }
        @RequestMapping(value = "/getTechList", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
         public @ResponseBody List<Technology> getTechList(){

             return getTechList();
            } 

        }

This is my Model class EnsureCapacity.java From this i am returning values into spring controller from that to my view page,While running getting stackoverflow exception .not understanding where i am wrong

package com.reveal.web.controller;

import java.util.ArrayList;
import java.util.List;

public class EnsureCapacity {

    List<Technology> techList = new ArrayList<Technology>(1000);

    public List<Technology> getTechList() {
        if(techList.isEmpty()){      
            ((ArrayList<Technology>) techList).ensureCapacity(1000);
            techList.add(new Technology(1, 0,"Node 1"));
            techList.add(new Technology(0, 1,"Node 1.1"));
            techList.add(new Technology(0, 1,"Node 1.2"));

            techList.add(new Technology(1, 0,"Node 2"));
            techList.add(new Technology(0, 1,"Node 2.1"));
            techList.add(new Technology(0, 1,"Node 2.2"));
            techList.add(new Technology(0, 1,"Node 2.3"));
            techList.add(new Technology(1, 0,"Node 3"));
            techList.add(new Technology(0, 1,"Node 3.1"));
            techList.add(new Technology(0, 1,"Node 3.2"));


            techList.add(new Technology(1, 0,"Node 4"));
            techList.add(new Technology(0, 1,"Node 4.2"));
            techList.add(new Technology(0, 1,"Node 4.3"));
            techList.add(new Technology(0, 1,"Node 4.4"));

            techList.add(new Technology(1, 0,"Node 5"));
            techList.add(new Technology(0, 1,"Node 4.5"));
            techList.add(new Technology(0, 1,"Node 4.6"));
            techList.add(new Technology(1, 0,"Node 6"));

            techList.add(new Technology(0, 1,"Node 6.1"));

            techList.add(new Technology(0, 1,"Node 6.2"));

            techList.add(new Technology(0, 1,"Node 6.3"));

            techList.add(new Technology(0, 1,"Node 6.4"));

            techList.add(new Technology(0, 1,"Node 6.5"));

            techList.add(new Technology(0, 1,"Node 6.6"));

            techList.add(new Technology(0, 1,"Node 6.7"));

            techList.add(new Technology(0, 1,"Node 6.8"));
            techList.add(new Technology(1, 0,"Node 7"));
            techList.add(new Technology(0, 1,"Node 7.1"));
            techList.add(new Technology(1, 0,"Node 8"));
            techList.add(new Technology(0, 1,"Node 8.1"));
            techList.add(new Technology(0, 1,"Node 8.2"));
            techList.add(new Technology(0, 1,"Node 8.3"));
            techList.add(new Technology(0, 1,"Node 8.4"));
            techList.add(new Technology(0, 1,"Node 8.5"));
            techList.add(new Technology(0, 1,"Node 8.6"));
            techList.add(new Technology(0, 1,"Node 8.7"));
            techList.add(new Technology(0, 1,"Node 8.8"));
            techList.add(new Technology(0, 1,"Node 8.9"));

            techList.add(new Technology(0, 1,"Node 8.10"));
            techList.add(new Technology(0, 1,"Node 8.11"));
            techList.add(new Technology(0, 1,"Node 8.12"));
            techList.add(new Technology(0, 1,"Node 8.12"));
        }
        return techList;
    }


    public void setTechList(List<Technology> techList) {
        this.techList = techList;
    }

    class Technology {

        private int Id;
        private int pId;
        private String techName;

        public int getId() {
            return Id;
        }

        public void setId(int Id) {
            this.Id = Id;
        }

        public int getpId() {
            return pId;
        }

        public void setpId(int pId) {
            this.pId = pId;
        }

        public String getTechName() {
            return techName;
        }

        public void setTechName(String techName) {
            this.techName = techName;
        }

        public Technology(int Id, int pId, String techName) {
            this.Id = Id;
            this.pId = pId;
            this.techName = techName;
        }
    }
}`
1

There are 1 best solutions below

0
On BEST ANSWER

Here you call the method getTechListof your controller in an endless loop wich causes the stackoverflowException.

 public @ResponseBody List<Technology> getTechList(){

         return getTechList();
        } 

You have to call: new EnsureCapacity().getTechList(); instead of getTechList();