我有一个弹簧休息应用程序,它有两个带有双向关系的实体(一对多,多对一)。为了克服嵌套的提取问题,@ JsonManagedReference / @ JsonBackReference已用于实体之间的perent / child关系。
这些人看起来像这样:
@Entity
@Table(name = "Parent")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Parent implements java.io.Serializable {
private Integer id;
private List<Child> childList;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@JsonManagedReference
public List<Child> getChildList() {
return childList;
}
public void setChildListe(List<Child> childListe) {
this.childList = childList;
}
}
@Entity
@Table(name = "Child")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Child implements java.io.Serializable {
private Integer id;
private Parent parent;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ParentID")
@JsonBackReference
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
这在获取Parent元素时工作正常,然后将childset同时提取并显示为json数组。
但是,由于使用了jsonbackreferance,子元素中没有父元素的引用。
我希望在json响应中获取附加到child-elemens的父对象,或者在通过休息请求单个子元素时至少获取父ID。
所有反馈都将受到关注:)
答案 0 :(得分:1)
我有一个类似的问题,我通过使用自定义序列化器/反序列化器解决了它
这就是我的处理方式(请根据您自己的代码进行调整):
<强> ChildSerializer 强>
public class ChildSerializer extends StdSerializer<Child> {
private static final long serialVersionUID = -265706839304575646L;
public ChildSerializer(Class<Child> t) {
super(t);
}
public ChildSerializer() {
this(null);
}
@Override
public void serialize(Child child, JsonGenerator jg, SerializerProvider sp)
throws IOException, JsonGenerationException {
jg.writeStartObject();
jg.writeStringField("name", child.getName());
jg.writeStringField("surname", child.getSurname());
Parent parent = child.getParent();
jg.writeObjectFieldStart("parent");
jg.writeStringField("name", parent.getName());
jg.writeStringField("surname", parent.getSurname());
jg.writeEndObject();
}
}
儿童强>
@JsonSerialize(using = ChildSerializer.class)
public class Child implements Serializable {
private static final long serialVersionUID = 7902561110976676934L;
private String name;
private String surname;
private Parent parent;
public Child(String name, String surname, Parent parent) {
this(name, surname);
this.parent = parent;
}
public Child(String name, String surname) {
this();
this.name = name;
this.surname = surname;
}
public Child() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((parent == null) ? 0 : parent.hashCode());
result = prime * result + ((surname == null) ? 0 : surname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Child other = (Child) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (parent == null) {
if (other.parent != null)
return false;
} else if (!parent.equals(other.parent))
return false;
if (surname == null) {
if (other.surname != null)
return false;
} else if (!surname.equals(other.surname))
return false;
return true;
}
}
<强>父强>
public class Parent implements Serializable {
private static final long serialVersionUID = -5604725691780073247L;
private String name;
private String surname;
private List<Child> childs;
public Parent(String name, String surname) {
this();
this.name = name;
this.surname = surname;
}
public Parent(String name, String surname, List<Child> childs) {
this(name, surname);
this.childs = childs;
}
public Parent() {
super();
childs = new ArrayList<Child>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void addChild( Child child )
{
if( !childs.contains(child) )
{
child.setParent(this);
childs.add(child);
}
}
public List<Child> getChilds() {
return childs;
}
public void setChilds(List<Child> childs) {
this.childs = childs;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((childs == null) ? 0 : childs.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((surname == null) ? 0 : surname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Parent other = (Parent) obj;
if (childs == null) {
if (other.childs != null)
return false;
} else if (!childs.equals(other.childs))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (surname == null) {
if (other.surname != null)
return false;
} else if (!surname.equals(other.surname))
return false;
return true;
}
}
我的测试方法:
@Test
public void testJson()
{
try
{
Parent p = new Parent(UUID.randomUUID().toString(), UUID.randomUUID().toString());
for (int i = 0; i < 10; i++) {
p.addChild(new Child(UUID.randomUUID().toString(), UUID.randomUUID().toString()));
}
ObjectMapper om = new ObjectMapper();
String childJson = om.writeValueAsString(p.getChilds().get(0));
System.out.println(childJson);
} catch (Exception e)
{
e.printStackTrace();
}
}
最终输出是:
{"name":"b86eab86-9858-4536-9c5c-d44d22810fc1","surname":"9a6249f0-58df-44e5-a1b9-31fbad6e9f49","parent":{"name":"74b0cd97-64a1-4547-ab22-4e4eedd0759b","surname":"a33c79f3-6f26-478b-9e24-7df96b3b1f68"}}
可悲的是,注释是强大的,但并不总是它们允许你获得你需要的东西,所以你必须自己做#34;
我希望这可以是有用的