首页 文章

如果值为null,如何告诉Jackson在序列化期间忽略某个字段?

提问于
浏览
574

如果该字段的值为null,Jackson如何配置为在序列化期间忽略字段值 .

例如:

public class SomeClass {
   // what jackson annotation causes jackson to skip over this value if it is null but will 
   // serialize it otherwise 
   private String someValue; 
}

16 回答

  • 1

    使用Jackson> 1.9.11和<2.x使用 @JsonSerialize 注释来执行此操作:

    @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

  • 0

    如果要将此规则添加到Jackson 2.6中的所有模型,请使用:

    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    
  • 5

    这在 Spring boot 2.0.3 and Jackson 2.0 为我工作

    import com.fasterxml.jackson.annotation.JsonInclude;
    
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class ApiDTO
    {
        // your class variable and 
        // methods
    }
    
  • 1
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    

    应该管用 .

    Include.NON_EMPTY 表示如果属性的值不为null且不为空,则序列化该属性 . Include.NON_NULL 表示如果属性值不为null,则序列化该属性 .

  • 101

    要使用Jackson> 2.0禁止使用空值序列化属性,您可以configure the ObjectMapper directly,或使用@JsonInclude注释:

    mapper.setSerializationInclusion(Include.NON_NULL);
    

    要么:

    @JsonInclude(Include.NON_NULL)
    class Foo
    {
      String bar;
    }
    

    或者,您可以在getter中使用 @JsonInclude ,以便在值不为null时显示该属性 .

    my answerHow to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson中提供了更完整的示例 .

  • 32

    如果使用Spring,则为全局配置

    @Configuration
    public class JsonConfigurations {
    
        @Bean
        public Jackson2ObjectMapperBuilder objectMapperBuilder() {
            Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
            builder.serializationInclusion(JsonInclude.Include.NON_NULL);
            builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
            builder.failOnUnknownProperties(false);
            return builder;
        }
    
    }
    
  • 10

    Jackson 2.x使用

    mapper.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_NULL);
    
  • 1

    这已经困扰了我很长一段时间,我终于找到了这个问题 . 问题是由于导入错误 . 早些时候我一直在使用

    com.fasterxml.jackson.databind.annotation.JsonSerialize
    

    哪个已被弃用 . 只需替换导入

    import org.codehaus.jackson.map.annotate.JsonSerialize;
    import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
    

    并用它作为

    @JsonSerialize(include=Inclusion.NON_NULL)
    
  • 7

    在我的情况下

    @JsonInclude(Include.NON_EMPTY)
    

    使它工作 .

  • 5

    您可以使用以下映射器配置:

    mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
    

    自2.5以来你可以用户:

    mapper.setSerializationInclusion(Include.NON_NULL);
    
  • 947

    在Jackson 2.x中,使用:

    @JsonInclude(JsonInclude.Include.NON_NULL)
    
  • 2

    只是为了扩展其他答案 - 如果你需要在每个字段的基础上控制遗漏空值,请注释相关字段(或者注释字段的'getter') .

    example - 这里只有 fieldOne 将从json中省略,如果它为null . 无论是否为空,都将包含 fieldTwo .

    public class Foo {
    
        @JsonInclude(JsonInclude.Include.NON_NULL) 
        private String fieldOne;
    
        private String fieldTwo;
    }
    

    要将类中的所有空值省略为默认值,请注释该类 . 如有必要,仍可使用按字段/ getter注释覆盖此默认值 .

    example - 这里 fieldOnefieldTwo 将分别从json中省略,因为这是类注释的默认设置 . 但是,由于字段上的注释, fieldThree 将覆盖默认值并始终包含在内 .

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public class Foo {
    
        private String fieldOne;
    
        private String fieldTwo;
    
        @JsonInclude(JsonInclude.Include.ALWAYS)
        private String fieldThree;
    }
    

    UPDATE

    以上是 Jackson 2 . 对于 earlier versions 的 Jackson 你需要使用:

    @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
    

    代替

    @JsonInclude(JsonInclude.Include.NON_NULL)
    

    如果这个更新很有用,请在下面提到ZiglioUK的答案,它在我更新我的答案之前很久就指出了更新的Jackson 2注释!

  • 10

    对于Jackson 2.5使用:

    @JsonInclude(content=Include.NON_NULL)
    
  • 55

    如果在 Spring Boot 中,您可以直接通过属性文件自定义jackson ObjectMapper .

    示例 application.yml

    spring:
      jackson:
        default-property-inclusion: non_null # only include props if non-null
    

    可能的值是:

    always|non_null|non_absent|non_default|non_empty
    

    更多:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html#howto-customize-the-jackson-objectmapper

  • 0

    如果您正在尝试序列化对象列表并且其中一个为null,那么您最终将在json中包含null项,即使

    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    

    将导致:

    [{myObject的},空]

    得到这个:

    [{myObject的}]

    人们可以这样做:

    mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object obj, JsonGenerator jsonGen, SerializerProvider unused)
                    throws IOException
            {
                //IGNORES NULL VALUES!
            }
        });
    

    提示:如果您正在使用DropWizard,则可以使用environment.getObjectMapper()检索Jersey使用的ObjectMapper .

  • 115

    你可以设置 application.properties

    spring.jackson.default-property-inclusion=non_null
    

    application.yaml

    spring:
      jackson:
        default-property-inclusion: non_null
    

    http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

相关问题