我想根据某些标记忽略 fastrxml @JsonProperty
中的ObjectMapper
年度
例如:
public class FeedData implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@JsonProperty(value="n")
private String name;
@JsonProperty(value="i")
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
我有一个基于输出的标志应该变化
如果标志是true
比json应该是{"n":"Radha","i":1}
否则如果标志是false
比json应该是{"name":"Radha","id":1}
答案 0 :(得分:0)
您可以根据标志值配置对象映射器:
ObjectMapper mapper = new ObjectMapper();
if (!flag) {
mapper.setAnnotationIntrospector( new JacksonAnnotationIntrospector() {
@Override
public PropertyName findNameForSerialization(Annotated a) {
return null;
}
});
}
mapper.convertValue(object, FeedData.class)