如何指定jackson只使用字段 - 最好是全局

时间:2011-08-18 10:10:32

标签: java json jackson

默认的jackon行为似乎使用两个属性(getter和setter)和字段来序列化和反序列化为json。

我想使用这些字段作为序列化配置的规范来源,因此根本不希望jackson查看属性。

我可以使用注释在单个类的基础上执行此操作:

 @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)

但我不想把它放在每一个班级......

是否可以全局配置?比如在Object Mapper中加一些吗?

8 个答案:

答案 0 :(得分:137)

您可以像这样配置各个ObjectMappers:

ObjectMapper mapper  = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

如果要全局设置,我通常会通过包装类访问已配置的映射器。

答案 1 :(得分:137)

在Jackson 2.0及更高版本中,您只需使用:

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;   

...

ObjectMapper mapper = new ObjectMapper();    
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

关闭自动检测。

答案 2 :(得分:32)

特别适用于boolean is*()获取者:

我花了很多时间来讨论为什么不在

之下
  @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)

也不是这个

  setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE);
  setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE);
  setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

为我的布尔Getter / Setter工作。

解决方案很简单:

  @JsonAutoDetect(isGetterVisibility = Visibility.NONE, ...          
  setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);

更新:允许spring-boot配置它:

jackson:
  visibility.field: any
  visibility.getter: none
  visibility.setter: none
  visibility.is-getter: none

请参阅Common application properties # JACKSON

答案 3 :(得分:12)

杰克逊1.9.10我用

ObjectMapper mapper = new ObjectMapper();

mapper.setVisibility(JsonMethod.ALL, Visibility.NONE);
mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);

关闭自动扣除。

答案 4 :(得分:10)

这个怎么样:我用mixin

不合规对象

@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
public static class TelemetryMixin {}

密新:

    ObjectMapper om = objectMapper.addMixIn(Telemetry.class, TelemetryMixin.class);
    Telemetry[] telemetries = om.readValue(someJson, Telemetry[].class);

用法:

value_list = [1, 4, 5, 6, 54]

没有任何内容表明你无法预约任何数量的课程并应用相同的混音。

如果你不熟悉mixins,它们在概念上很简单:mixin的结构是超级强加在目标类上(根据jackson,而不是JVM) 。

答案 5 :(得分:3)

如果您使用Spring Boot,则可以按以下方式全局配置杰克逊

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

@Configuration
public class JacksonObjectMapperConfiguration implements Jackson2ObjectMapperBuilderCustomizer {

    @Override
    public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
        jacksonObjectMapperBuilder.visibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        jacksonObjectMapperBuilder.visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        jacksonObjectMapperBuilder.visibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY);
    }

}

答案 6 :(得分:1)

如果您想在全球范围内执行此操作而不必担心ObjectMapper的配置,则可以创建自己的注释:

@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonAutoDetect(
        getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE,
        setterVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE,
        creatorVisibility = JsonAutoDetect.Visibility.NONE
)
public @interface JsonExplicit {
}

现在,您只需要用@JsonExplicit注释您的课程,就可以开始学习了!

还请确保编辑上面对@JsonAutoDetect的调用,以确保您将值设置为与程序兼容的值。

感谢https://stackoverflow.com/a/13408807的帮助,以帮助我了解@JacksonAnnotationsInside

答案 7 :(得分:0)

@since 2.10版本,我们可以使用JsonMapper.Builder,接受的答案可能类似于:

JsonMapper mapper = JsonMapper.builder()
    .visibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
    .visibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
    .visibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE)
    .visibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.NONE)
    .build();