泽西/杰克逊@JsonIgnore在二传手 [英] Jersey/Jackson @JsonIgnore on setter
问题描述
我有一个带有以下注释的类:
i have an class with the following annotations:
class A {
public Map<String,List<String>> references;
@JsonProperty
public Map<String,List<String>> getReferences() {
...
}
@JsonIgnore
public void setReferences(Map<String,List<String>>) {
}
...
}
}
我尝试的是什么是在反序列化时忽略json。但它不起作用。始终当JSON String到达时,Jackson lib会填充references属性。如果我只使用@JsonIgnore注释,则getter不起作用。有没有解决这个问题的方法?
What I try is to ignore the json on deserialization. But it doesn't work. Always when JSON String arrives the Jackson lib fill the references attribute. If I use only the @JsonIgnore annotation the getter doesn't work. Are there any solutions for this problem?
谢谢
推荐答案
我我认为有两个关键部分可以让你根据需要拥有只读集合。首先,除了忽略setter之外,还要确保字段还标有 @JsonIgnore
:
I think there are two key pieces that should enable you to have "read-only collections" as desired. First, in addition to ignoring the setter, ensure that your field is also marked with @JsonIgnore
:
class A {
@JsonIgnore
public Map<String,List<String>> references;
@JsonProperty
public Map<String,List<String>> getReferences() { ... }
@JsonIgnore
public void setReferences(Map<String,List<String>>) { ... }
}
其次,为了防止getter被用作setter,请禁用 USE_GETTERS_AS_SETTERS
功能:
Second, in order to prevent the getters from being used as setters, disable the USE_GETTERS_AS_SETTERS
feature:
ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.USE_GETTERS_AS_SETTERS);
这篇关于泽西/杰克逊@JsonIgnore在二传手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!