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/