gpt4 book ai didi

java - Json (fasterxml) stackoverflow 异常

转载 作者:搜寻专家 更新时间:2023-10-31 08:09:47 25 4
gpt4 key购买 nike

当尝试序列化一个类别时,我得到一个 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)

类别.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<Category> getParentChain()
{

List<Category> cats = new ArrayList<>();
Category current = this;
while(!current.isMainCategory())
{
cats.add(current);
current = current.getParent();
}
cats.add(current);
Collections.reverse(cats);
return cats;
}

@Override
public String toString()
{
return this.title;
}

@Override
public boolean equals(Object o)
{
if(!(o instanceof Category))return false;
Category c = (Category)o;

return c.title.equals(this.title);
}

@Override
public int hashCode()
{
return super.hashCode();
}
}

休息 Controller 功能

@RequestMapping(value="/{id}", method=RequestMethod.GET)
public ResponseEntity<Category> get(@PathVariable("id") long categoryId)
{
Category c = service.getCategoryRepository().ReadValue(categoryId);
if(c == null)
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
return new ResponseEntity<>(c,HttpStatus.OK);
}

注意

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

它与我的其他类一起工作正常,只有这个类会导致 stackoverflow。

最佳答案

当然,@JsonIgnore 可以完成这项工作。但是,如果我们需要在 JSON 输出中忽略字段怎么办?

解决方案非常简单。

我们通过关系一侧的 @JsonManagedReference 注释来注释我们的“有罪”字段(这意味着我们的 @ManyToMany 注释)。

并且 @JsonBackReference 在关系的另一边(@OneToMany 已被放置)。

就是这样。不再有递归循环。

关于java - Json (fasterxml) stackoverflow 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31841666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com