首页 文章

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

提问于
浏览 26 次
158

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

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

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

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

但我不想把它放在每一堂课......

是否可以在全球范围内进行配置?喜欢在Object Mapper中加一些?

5 回答

  • 126

    您可以像这样配置各个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));
    

    如果你想全局设置,我通常通过包装类访问配置的映射器 .

  • 25

    在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);
    

    关闭自动检测 .

  • 11

    Specifically for boolean is() getters:*

    我花了很多时间:为什么都没有

    @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);
    
  • 132

    对于 Jackson 1.9.10我用

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

    转向自动解除 .

  • 10

    怎么样:我用mixin来使用它

    不合规的对象

    @Entity
    @Getter
    @NoArgsConstructor
    public class Telemetry {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long pk;
        private String id;
        private String organizationId;
        private String baseType;
        private String name;
        private Double lat;
        private Double lon;
        private Instant updateTimestamp;
    }
    

    混入:

    @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);
    

    没有任何东西可以说你无法预测任何数量的课程并应用相同的混音 .

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

相关问题