我试图从我的控制器中获取一个对象并在我的JSP中显示。在调试我的控制器时,返回正确的数据。但是当我使用firebug调试我的javascript时,流程不会进入getJson的函数(数据)。
$.getJSON('/em',{name: name, cv: cv},
function(data){
alert("Hello");
alert(data.name);
});
控制器 -
@RequestMapping(value = "/em", method = RequestMethod.GET)
public Employee addEmployee(@RequestParam(value = "name", required = true) String name, @RequestParam(value = "cv", required = true) String cv,Model model) {
return addedEm;
}
我在这里得到了正确的返回。不知道这里出了什么问题。有人可以帮我吗?感谢。
另外,当我将@ResponseBody添加到我的控制器时,我会在firebug上得到回复:
Apache Tomcat/7.0.12 - Error report - <h1>HTTP Status 500 - </h1><p><b>type</b> Exception report</p><u>The server encountered an internal error () that prevented it from fulfilling this request.</u><b>exception</b> <pre>org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.hibernate.collection.PersistentBag[0]-> nz.co.datacom.panelrequests.pd.user.Employee["user"]-> nz.co.datacom.panelrequests.pd.user.User["roles"]-> org.hibernate.collection.PersistentBag[0]-> nz.co.datacom.panelrequests.pd.user.Panelist["employees"]->*
然后:
org.codehaus.jackson.map.ser.BeanSerializer.serializeFields(BeanSerializer.java:189)
org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:142)
org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:442)
org.codehaus.jackson.map.ser.ContainerSerializers$CollectionSerializer.serializeContents(ContainerSerializers.java:383)
org.codehaus.jackson.map.ser.ContainerSerializers$AsArraySerializer.serialize(ContainerSerializers.java:142)
然后重复,直到萤火虫的最大限度。
答案 0 :(得分:4)
这种情况正在发生,因为您的实体互相引用。这是创造无限递归。杰克逊去的时候读一个,然后回到另一个然后回到那个等等。你的雇主提到雇员,你的雇员提到了雇主。 OneToMany和ManyToOne
这是一个带有修复的博客条目。 http://vard-lokkur.blogspot.com/2010/10/json-jackson-to-rescue.html
http://wiki.fasterxml.com/JacksonFeatureBiDirReferences
另外,如果您不希望它们包括在内,您还可以使用注释@JsonIgnore忽略实体的某些字段。