Pet*_*ete 53 java serialization json jackson
是否有一种内置的方法只能在使用Jackson(rapidxml.jackson 2.1.1)时序列化孩子的id?我们想发送一个Order有Person参考的REST .然而,person对象非常复杂,我们可以在服务器端刷新它,所以我们只需要主键.
或者我需要一个自定义序列化器吗?或者我需要@JsonIgnore所有其他属性吗?这会阻止Person数据在请求Order对象时被发回吗?我不确定我是否需要它,但如果可能的话我想控制它...
Sta*_*Man 104
有几种方法.第一个用于@JsonIgnoreProperties从子项中删除属性,如下所示:
public class Parent {
@JsonIgnoreProperties({"name", "description" }) // leave "id" and whatever child has
public Child child; // or use for getter or setter
}
Run Code Online (Sandbox Code Playgroud)
另一种可能性,如果Child对象始终序列化为id:
public class Child {
// use value of this property _instead_ of object
@JsonValue
public int id;
}
Run Code Online (Sandbox Code Playgroud)
还有一种方法是使用 @JsonIdentityInfo
public class Parent {
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@JsonIdentityReference(alwaysAsId=true) // otherwise first ref as POJO, others as id
public Child child; // or use for getter or setter
// if using 'PropertyGenerator', need to have id as property -- not the only choice
public int id;
}
Run Code Online (Sandbox Code Playgroud)
这也适用于序列化,并忽略id以外的属性.但是,结果不会被包装为Object.
| 归档时间: |
|
| 查看次数: |
22926 次 |
| 最近记录: |