如何忽略Jackson 2.2.3中的“Is”方法 [英] How to ignore "Is' methods with Jackson 2.2.3

查看:854
本文介绍了如何忽略Jackson 2.2.3中的“Is”方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的类有是方法和获取方法。我想杰克逊忽略调用所有是方法。

A simple class has "is" methods and "get" methods. I would like Jackson to ignore calling all "is" methods.

我尝试设置 ObjectMapper 的可见性设置为

I tried by setting ObjectMapper's visibility by setting as

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类:冲突Jackson 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中的“Is”方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆