我如何 return 与 Dropwizard/Hibernate/Swagger 多对多关系中的子对象而不递归?
How can I return child objects in a many-to-many relationship with Dropwizard/Hibernate/Swagger without recursion?
我正在使用 Dropwizard 和 Swagger 创建一个 REST API 来管理对各种对象的访问。其中两个对象涉及多对多关系,例如:
public class Puppy implements Serializable {
private Long id;
private String name;
@ManyToMany(targetEntity = Trick.class)
@JoinTable(
name="puppies_tricks",
joinColumns=@JoinColumn(name="puppy_id"),
inverseJoinColumns=@JoinColumn(name="trick_id"))
private List<Trick> tricks;
@JsonProperty("tricks")
public List<Trick> getTricks() { return this.tricks; }
...
}
public class Trick implements Serializable {
private Long id;
private String name;
@ManyToMany(targetEntity = Puppy.class)
@JoinTable(
name="puppies_tricks",
joinColumns=@JoinColumn(name="trick_id"),
inverseJoinColumns=@JoinColumn(name="puppy_id"))
private List<Puppy> puppies;
@JsonProperty("puppies")
public List<Puppy> getPuppies() { return this.puppies; }
...
}
假设实际数据类似于:
# Tricks:
[{ id: 1, name: 'Roll over' },
{ id: 2, name: 'Play dead' },
{ id: 3, name: 'Steal second' }]
# Puppies:
[{ id: 1, name: 'Flopsy' },
{ id: 2, name: 'Mopsy' },
{ id: 3, name: 'Cottontail' }]
# Puppies_Tricks
[{ puppy_id: 1, trick_id: 1 },
{ puppy_id: 1, trick_id: 2 },
{ puppy_id: 2, trick_id: 2 },
{ puppy_id: 2, trick_id: 3 }]
所以当我 GET /puppy/1 时,我想得到这样的结构:
{
"id": 1,
"name": "Flopsy",
"tricks": [
{ "id": 1, "name": "Roll over"},
{ "id": 2, "name": "Play dead" } ]
}
但我实际得到的是一个嵌套数组:
{
"id": 1,
"name": "Flopsy",
"tricks": [
{ "id": 1, "name": "Roll over", "puppies": [ { "id": 1, "name": "Flopsy" }, ...},
{ "id": 2, "name": "Play dead", "puppies": [...] } ]
}
有什么方法可以告诉 Jackson/Dropwizard 在得到小狗时停止通过技巧下降,反之亦然?
Dropwizard 1.0.6,Java1.8.
@JsonIgnore
@JsonProperty("puppies")
public List<Puppy> getPuppies() { return this.puppies; }
将停止无限循环
我的解决方案是创建一个单独的 PuppyCore class 来实现序列化但不持久化。这包括基本的 Puppy 信息(id、名称)。同样,TrickCore 只有 ID 和名称。
然后 Puppy 包含 Set 技巧,getter:
public Set<TrickCore> getTricks();
其中 returns 一个 TrickCore 对象列表 – 这些对象没有 Puppy 列表,因此停止递归。 Trick returns Set<PuppyCore>
for getPuppies()
.
的类似模式
Jackson 通过 @JsonManagedReference @JsonBackReference
注释支持对象图序列化。参见 the documentation here. You can find a simple example here。
您应该真的更仔细地阅读您所用工具的文档。
已经有an answer that summarizes可用的方法。
我正在使用 Dropwizard 和 Swagger 创建一个 REST API 来管理对各种对象的访问。其中两个对象涉及多对多关系,例如:
public class Puppy implements Serializable {
private Long id;
private String name;
@ManyToMany(targetEntity = Trick.class)
@JoinTable(
name="puppies_tricks",
joinColumns=@JoinColumn(name="puppy_id"),
inverseJoinColumns=@JoinColumn(name="trick_id"))
private List<Trick> tricks;
@JsonProperty("tricks")
public List<Trick> getTricks() { return this.tricks; }
...
}
public class Trick implements Serializable {
private Long id;
private String name;
@ManyToMany(targetEntity = Puppy.class)
@JoinTable(
name="puppies_tricks",
joinColumns=@JoinColumn(name="trick_id"),
inverseJoinColumns=@JoinColumn(name="puppy_id"))
private List<Puppy> puppies;
@JsonProperty("puppies")
public List<Puppy> getPuppies() { return this.puppies; }
...
}
假设实际数据类似于:
# Tricks:
[{ id: 1, name: 'Roll over' },
{ id: 2, name: 'Play dead' },
{ id: 3, name: 'Steal second' }]
# Puppies:
[{ id: 1, name: 'Flopsy' },
{ id: 2, name: 'Mopsy' },
{ id: 3, name: 'Cottontail' }]
# Puppies_Tricks
[{ puppy_id: 1, trick_id: 1 },
{ puppy_id: 1, trick_id: 2 },
{ puppy_id: 2, trick_id: 2 },
{ puppy_id: 2, trick_id: 3 }]
所以当我 GET /puppy/1 时,我想得到这样的结构:
{
"id": 1,
"name": "Flopsy",
"tricks": [
{ "id": 1, "name": "Roll over"},
{ "id": 2, "name": "Play dead" } ]
}
但我实际得到的是一个嵌套数组:
{
"id": 1,
"name": "Flopsy",
"tricks": [
{ "id": 1, "name": "Roll over", "puppies": [ { "id": 1, "name": "Flopsy" }, ...},
{ "id": 2, "name": "Play dead", "puppies": [...] } ]
}
有什么方法可以告诉 Jackson/Dropwizard 在得到小狗时停止通过技巧下降,反之亦然?
Dropwizard 1.0.6,Java1.8.
@JsonIgnore
@JsonProperty("puppies")
public List<Puppy> getPuppies() { return this.puppies; }
将停止无限循环
我的解决方案是创建一个单独的 PuppyCore class 来实现序列化但不持久化。这包括基本的 Puppy 信息(id、名称)。同样,TrickCore 只有 ID 和名称。
然后 Puppy 包含 Set 技巧,getter:
public Set<TrickCore> getTricks();
其中 returns 一个 TrickCore 对象列表 – 这些对象没有 Puppy 列表,因此停止递归。 Trick returns Set<PuppyCore>
for getPuppies()
.
Jackson 通过 @JsonManagedReference @JsonBackReference
注释支持对象图序列化。参见 the documentation here. You can find a simple example here。
您应该真的更仔细地阅读您所用工具的文档。
已经有an answer that summarizes可用的方法。