问题描述
一个简单的类有is"方法和get"方法.我希望 Jackson
忽略调用所有is"方法.
A simple class has "is" methods and "get" methods. I would like Jackson
to ignore calling all "is" methods.
我尝试通过设置为
mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);
但是还是在考虑is-getter
方法,为什么呢?
But it is still considering the is-getter
methods, why?
Jackson
序列化 getter 方法和公共变量.是否可以指示 Jackson
只调用公共 getter 方法而不是序列化变量?
Jackson
serializes getter methods and public variables. Is it possible to indicate Jackson
to call only public getter methods but not serialize variables?
推荐答案
你应该考虑 @JsonAutoDetect
注释.例如,(使用来自这个问题的 POJO 类:ConflictingJackson 2.2.3 解决方案中属性的 getter 定义可能如下所示:
You should consider @JsonAutoDetect
annotation. For example, (using POJO class from this question: Conflicting getter definitions for property in Jackson 2.2.3 solution could look like this:
@JsonAutoDetect(isGetterVisibility = Visibility.NONE)
class GetterMethodsObject {
private int id = 10;
public int getId() {
return id;
}
public boolean isId() {
return true;
}
}
示例用法:
ObjectMapper mapper = new ObjectMapper();
ObjectWriter objectWriter = mapper.writerWithDefaultPrettyPrinter();
System.out.println(objectWriter.writeValueAsString(new GetterMethodsObject()));
以上程序打印:
{
"id" : 10
}
这篇关于如何使用 Jackson 2.2.3 忽略“是"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!