java – Json(fasterxml)stackoverflow异常

前端之家收集整理的这篇文章主要介绍了java – Json(fasterxml)stackoverflow异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

尝试序列化类别时,我得到一个stackoverflow.

例外

Warning: StandardWrapperValve[dispatcher]: Servlet.service() for
servlet dispatcher threw exception java.lang.StackOverflowError at
java.lang.ClassLoader.defineClass1(Native Method) at
java.lang.ClassLoader.defineClass(ClassLoader.java:760) at
org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2279)
at
org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1501)
at
org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at
org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:660)
at
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
at
com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100)
at
com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21)
at
com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:183)
at
com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541)
at
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644)
at
com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)

Category.java

@Entity
public class Category implements DataObject,Serializable {

    @Id
    @GeneratedValue 
    private Long id;
    private String title;
    private String description;

    @ManyToOne @JsonIgnore 
    private Category parent;


@Override
public long getId() {
    return id;
}

@Override
public void setId(long id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public Category getParent() {
   return null;//return parent;
}

public void setParent(Category parent) {
   // this.parent = parent;
}

public boolean isMainCategory()
{
   return true;// return this.parent == null;
}

/**
 * Returns the chain of parent categories with the main category on index 0
 * @return Chain of categories 
 */
public List

休息控制器功能

@RequestMapping(value="/{id}",method=RequestMethod.GET)
public ResponseEntity

注意

即使我替换返回新的ResponseEntity<>(c,HttpStatus.OK);返回新的ResponseEntity<>(new Category(),HttpStatus.OK);我将得到一个stackoverflow whilist没有字段包含值.

它适用于我的其他类,它只是这个类导致stackoverflow.

最佳答案
如果您评论私人类别父母,可能你不会有StackOverflow.我在具有循环依赖项的项目中遇到了同样的问题.

解决此问题的最佳方法是使用父级的id而不是类,如:

private Long parentId;

编辑:

问题在于尝试序列化的getParentChain().通过在方法之前添加@JsonIgnore,问题得以解决.

原文链接:https://www.f2er.com/spring/431955.html

猜你在找的Spring相关文章