尝试序列化类别时,我得到一个 stackoverflow。
例外
警告:StandardWrapperValve[dispatcher]: Servlet Dispatcher 的 Servlet.service() 在 java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:760) 处抛出异常 java.lang.StackOverflowError org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2279) 在 org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1501) 在 org.apache.felix.framework.BundleWiringImpl。在 com.fasterxml 的 java.lang.ClassLoader.loadClass(ClassLoader.java:357) 的 org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955) 访问 $400(BundleWiringImpl.java:75)。 com 的 jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:660)。com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100) 在 com.fasterxml.jackson.databind 上的 fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)。 ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21) 在 com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:183) 在 com.fasterxml.jackson.databind.ser.BeanPropertyWriter。 com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java: com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:644) 152)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)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)fastxml.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)fastxml.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)
类别.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);
我会得到一个stackoverflow whilist,没有一个字段包含值。
它适用于我的其他类,只有这个类会导致堆栈溢出。