尝试序列化类别时,我得到一个stackoverflow。
异常
警告:StandardWrapperValve [dispatcher]:Servlet.service()for servlet调度程序抛出异常java.lang.StackOverflowError 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) 在 org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1501) 在 org.apache.felix.framework.BundleWiringImpl.access $ 400(BundleWiringImpl.java:75) 在 org.apache.felix.framework.BundleWiringImpl $ BundleClassLoader.loadClass(BundleWiringImpl.java:1955) 在java.lang.ClassLoader.loadClass(ClassLoader.java:357)at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:660) 在 com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152) 在 com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100) 在 com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21) 在 com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:183) 在 com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:541) 在 com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644) 在 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<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();
}
}
休息控制器功能
@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);
,我也会得到一个堆栈溢出,但没有一个字段包含值。
它适用于我的其他类,它只是导致堆栈溢出的类。
答案 0 :(得分:10)
当然,@JsonManagedReference
完成了这项工作。但是如果我们在JSON输出中需要忽略字段呢?
解决方案非常简单。
我们通过@ManyToMany
注释在我们关系的一侧注释我们的'有罪'字段(这意味着我们的@JsonBackReference
注释)。
@OneToMany
位于关系的另一边(equations and all polylines
已被放置)。
就是这样。没有更多的递归循环。
答案 1 :(得分:4)
如果你评论private Category parent;
,你可能没有StackOverflow。我在具有循环依赖关系的项目中遇到了同样的问题。
解决此问题的最佳方法是使用父级的id而不是类,如:
private Long parentId;
编辑:
问题在于getParentChain()
尝试序列化。通过在方法之前添加@JsonIgnore来解决问题。
答案 2 :(得分:3)
一个注释可以解决您的问题。
在课堂上添加以下注释。
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id")
其他方法是在集合 @JsonManagedReference 上为正向指示, @JsonBackReference。在映射中向后指示。
示例:
public class User{
@JsonManagedReference
@OneToMany(mappedBy = "user")
Set<Address> s = new Hashset<>();
}
public class Address{
@JsonBackReference
@ManyToOne
@JoinColumn
User user;
}
答案 3 :(得分:0)
这就是我为避免这种递归地狱所做的事情。
向您的JPA实体中的每个@JsonIgnore
添加@OneToMany(mappedBy="xxxx")
JsonIgnore来自jackson-annotations
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.0</version>
</dependency>
JPA实体示例:
package model;
import java.io.Serializable;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.List;
/**
* The persistent class for the categoria database table.
*
*/
@Entity
@NamedQuery(name="Categoria.findAll", query="SELECT c FROM Categoria c")
@XmlRootElement(name = "categoria")
public class Categoria implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="categoria_id")
private int categoriaId;
private String descripcion;
@JsonIgnore
//bi-directional many-to-one association to Establecimiento
@OneToMany(mappedBy="categoria")
private List<Establecimiento> establecimientos;
public Categoria() {
}
public int getCategoriaId() {
return this.categoriaId;
}
public void setCategoriaId(int categoriaId) {
this.categoriaId = categoriaId;
}
public String getDescripcion() {
return this.descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public List<Establecimiento> getEstablecimientos() {
return this.establecimientos;
}
public void setEstablecimientos(List<Establecimiento> establecimientos) {
this.establecimientos = establecimientos;
}
public Establecimiento addEstablecimiento(Establecimiento establecimiento) {
getEstablecimientos().add(establecimiento);
establecimiento.setCategoria(this);
return establecimiento;
}
public Establecimiento removeEstablecimiento(Establecimiento establecimiento) {
getEstablecimientos().remove(establecimiento);
establecimiento.setCategoria(null);
return establecimiento;
}
}