gpt4 book ai didi

json - Play 框架 - JSON 序列化循环引用(例如 Files->User, User->Files)--> Stackoverflow

转载 作者:行者123 更新时间:2023-12-04 18:42:43 26 4
gpt4 key购买 nike

我已经玩了几天的 Play Framework ,但遇到了一个我无法解决的问题:假设您有一些“用户”和"file"类。两者相互引用并使用 ebean (@ManyToOne, @OneToMany) 持久化在数据库中。现在,当将用户作为 JSON 返回时,我收到一个 stackoverflow 错误。这是游戏中的错误还是我遗漏了什么?

@Entity
public class File extends Model {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
public Long id;

@ManyToOne(cascade=CascadeType.ALL)
public User author;
}
@Entity
public class User extends Model {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
public Long id;

@OneToMany(mappedBy="author", cascade=CascadeType.ALL)
public List<File> files;
}
import play.libs.Json;
public class Users extends Controller {
public static Result getJSON() {
List<User> users = new Model.Finder<String, User>(String.class, User.class).all();
return ok(Json.toJson(users));
}
}

堆栈跟踪:
org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: 
models.User["files"]->com.avaje.ebean.common.BeanList[0]->models.File["author"]->
models.User["files"]->com.avaje.ebean.common.BeanList[0]->models.File["author"]->
models.User["files"]->com.avaje.ebean.common.BeanList[0]->models.File["author"]->
models.User["files"]->com.avaje.ebean.common.BeanList[0]->models.File["author"]->
models.User["files"]->com.avaje.ebean.common.BeanList[0]->models.File["author"]->
models.User["files"]->com.avaje.ebean.common.BeanList[0]->models.File["author"]->
models.User["files"]->com.avaje.ebean.common.BeanList[0]->models.File["author"]->
models.User["files"]->com.avaje.ebean.common.BeanList[0]->models.File["author"]->
.
.
.
->models.User["files"])
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:164) ~[jackson-mapper-asl.jar:1.9.10]
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) ~[jackson-mapper-asl.jar:1.9.10]
at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) ~[jackson-mapper-asl.jar:1.9.10]
at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) ~[jackson-mapper-asl.jar:1.9.10]
at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) ~[jackson-mapper-asl.jar:1.9.10]
at org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72) ~[jackson-mapper-asl.jar:1.9.10]
java.lang.StackOverflowError: null
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.7.0_45]
at java.lang.ClassLoader.defineClass(ClassLoader.java:800) ~[na:1.7.0_45]
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.7.0_45]
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) ~[na:1.7.0_45]
at java.net.URLClassLoader.access$100(URLClassLoader.java:71) ~[na:1.7.0_45]
at java.net.URLClassLoader$1.run(URLClassLoader.java:361) ~[na:1.7.0_45]

最佳答案

好的,经过数小时的谷歌搜索,我找到了解决方案:
有一个注释@JsonIgnore(从 org.codehaus.jackson.annotate.JsonIgnore 导入)它告诉 JSON 方法忽略这个属性。

编辑:进一步挖掘,在 JSON 序列化期间处理循环引用的更清晰的解决方案是编写自定义的 De- 和 Serializer。 Steve Hill 的一篇非常好的帖子可以在这里找到:http://dev.sghill.net/2012/04/how-do-i-write-jackson-json-serializer.html

关于json - Play 框架 - JSON 序列化循环引用(例如 Files->User, User->Files)--> Stackoverflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839698/

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