我想忽略基于某个标志的fastrxml ObjectMapper
中的@JsonProperty
例如:
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}
您可以根据标志值配置对象映射器:
ObjectMapper mapper = new ObjectMapper();
if (!flag) {
mapper.setAnnotationIntrospector( new JacksonAnnotationIntrospector() {
@Override
public PropertyName findNameForSerialization(Annotated a) {
return null;
}
});
}
mapper.convertValue(object, FeedData.class)