Json (fasterxml) 计算器异常
Json (fasterxml) stackoverflow exception
尝试序列化类别时出现计算器溢出问题。
异常
Warning: StandardWrapperValve[dispatcher]: Servlet.service() for
servlet dispatcher threw exception java.lang.WhosebugError 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.access0(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<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);
,我也会得到一个 Whosebug whilist none 字段包含一个值。
它与我的其他 classes 一起工作正常,只是这个 class 导致了 Whosebug。
如果您发表评论 private Category parent;
,您可能不会拥有 Whosebug。我在一个有循环依赖的项目中遇到了同样的问题。
解决这个问题的最好方法是使用父代的 id 而不是 Class 像:
private Long parentId;
编辑:
问题出在试图序列化的 getParentChain()
上。通过在方法前添加@JsonIgnore 问题得到解决。
没问题,@JsonIgnore
就可以了。但是,如果我们需要在 JSON 输出中忽略字段怎么办?
解决方法很简单。
我们在关系的一侧用 @JsonManagedReference
注释来注释我们的 'guilty' 字段(这意味着我们的 @ManyToMany
注释)。
和 @JsonBackReference
在关系的另一边(@OneToMany
已被放置)。
就是这样。没有更多的递归循环。
一个注解解决了你的问题。
在 class 上添加以下注释。
@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;
}
这就是我为避免这种递归地狱所做的事情。
将 @JsonIgnore
添加到您的 JPA 实体中的每个 @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;
}
}
尝试序列化类别时出现计算器溢出问题。
异常
Warning: StandardWrapperValve[dispatcher]: Servlet.service() for servlet dispatcher threw exception java.lang.WhosebugError 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.access0(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<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);
,我也会得到一个 Whosebug whilist none 字段包含一个值。
它与我的其他 classes 一起工作正常,只是这个 class 导致了 Whosebug。
如果您发表评论 private Category parent;
,您可能不会拥有 Whosebug。我在一个有循环依赖的项目中遇到了同样的问题。
解决这个问题的最好方法是使用父代的 id 而不是 Class 像:
private Long parentId;
编辑:
问题出在试图序列化的 getParentChain()
上。通过在方法前添加@JsonIgnore 问题得到解决。
没问题,@JsonIgnore
就可以了。但是,如果我们需要在 JSON 输出中忽略字段怎么办?
解决方法很简单。
我们在关系的一侧用 @JsonManagedReference
注释来注释我们的 'guilty' 字段(这意味着我们的 @ManyToMany
注释)。
和 @JsonBackReference
在关系的另一边(@OneToMany
已被放置)。
就是这样。没有更多的递归循环。
一个注解解决了你的问题。
在 class 上添加以下注释。
@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;
}
这就是我为避免这种递归地狱所做的事情。
将 @JsonIgnore
添加到您的 JPA 实体中的每个 @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;
}
}