spring Controller 中的 java.lang.StackOverflowError

标签 java ajax angularjs jsp spring-mvc

TreesController.java 这是我的 Controller Methodenter 代码

    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();
            } 

        }

这是我的模型类 EnsureCapacity.java 从这里我将值返回到 spring Controller 中,从该 Controller 到我的 View 页面,在运行时获取 stackoverflow 异常。不明白我错在哪里

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;
        }
    }
}`

最佳答案

在这里,您在无限循环中调用 Controller 的方法getTechList,这会导致 stackoverflowException。

 public @ResponseBody List<Technology> getTechList(){

         return getTechList();
        } 

您必须调用:new EnsureCapacity().getTechList(); 而不是 getTechList();

关于spring Controller 中的 java.lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30725990/

相关文章:

javascript - 如何合并两个过滤器(AND运算)

java - Spring 应用程序不会在包之外启动

javascript - jQuery 数组设置键和值并通过 AJAX 发送数组

动态加载图像的 JQuery .on() 事件处理程序用法

java - JSF GraphicImage onclick 问题

javascript - 使用node.js和angularjs将未经身份验证的用户重定向到默认URL

java - 使用 Spring Boot 初始化 AWS

java - AVD 无法正常工作。它出现了但无法解决

java - 在操作栏上按 "back"时 Android 应用程序崩溃

javascript - angular.module ('myapp',['chart.js']) Uncaught Error : [$injector:modulerr]