我有以下类:Jackson JSON - @JsonProperty重写@JsonView?
class Bean {
private int x;
private int y;
public Bean(int x, int y) {
this.x = x;
this.y = y;
}
@JsonProperty("x")
@JsonView(View1.class)
public void setX(int x) {
this.x = x;
}
public int getX() {
return this.x;
}
@JsonProperty("y")
public void setY(int y) {
this.y = y;
}
public int getY() {
return this.y;
}
}
和视图:
class View1 {
}
我希望_mapper.writerWithView(View1.class).writeValueAsString(bean)
返回{"x":1}
,因为x
是具有连接到它的视图的唯一价值,但我获得{"x":1,"y":2}
。
为什么要这样做,我该如何让它不这样做?我知道我可以删除@JsonProperty("y")
,但是如果我选择使用普通writer而不是writerWithView,我仍然可以序列化所有值。
是U确保Y不被设置在其他地方? – sidgate