本文翻译自:How to tell Jackson to ignore a field during serialization if its value is null?
How can Jackson be configured to ignore a field value during serialization if that field's value is null. 如果该字段的值为null,Jackson如何配置为在序列化期间忽略字段值。
For example: 例如:
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;
}
#1楼
参考:https://stackoom.com/question/nKet/如果值为null-如何告诉Jackson在序列化期间忽略某个字段
#2楼
To suppress serializing properties with null values using Jackson >2.0, you can configure the ObjectMapper
directly , or make use of the @JsonInclude
annotation: 要使用Jackson> 2.0禁止使用空值序列化属性,可以直接配置ObjectMapper
,或者使用@JsonInclude
批注:
mapper.setSerializationInclusion(Include.NON_NULL);
or: 要么:
@JsonInclude(Include.NON_NULL)
class Foo
{
String bar;
}
Alternatively, you could use @JsonInclude
in a getter so that the attribute would be shown if the value is not null. 或者,您可以在getter中使用@JsonInclude
,以便在值不为null时显示该属性。
A more complete example is available in my answer to How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson . 我的答案中提供了一个更完整的示例: 如何防止Map中的空值和bean内的空字段通过Jackson序列化 。
#3楼
You can use the following mapper configuration: 您可以使用以下映射器配置:
mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
Since 2.5 you can user: 从2.5开始你可以用户:
mapper.setSerializationInclusion(Include.NON_NULL);
#4楼
使用Jackson> 1.9.11和<2.x使用@JsonSerialize
注释来执行此操作:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
#5楼
Just to expand on the other answers - if you need to control the omission of null values on a per-field basis, annotate the field in question (or alternatively annotate the field's 'getter'). 只是为了扩展其他答案 - 如果你需要在每个字段的基础上控制遗漏空值,请注释相关字段(或者注释字段的'getter')。
example - here only fieldOne
will be ommitted from json if it is null. 示例 -此处只有fieldOne
将从json中省略,如果它为null。 fieldTwo
will always be included regardless of if it is null. fieldTwo
将始终包含在内,无论它是否为null。
public class Foo {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String fieldOne;
private String fieldTwo;
}
To omit all null values in the class as a default, annotate the class. 要将类中的所有空值省略为默认值,请注释该类。 Per-field/getter annotations can still be used to override this default if necessary. 如有必要,仍可使用按字段/ getter注释覆盖此默认值。
example - here fieldOne
and fieldTwo
will be ommitted from json if they are null, respectively, because this is the default set by the class annotation. 示例 -这里fieldOne
和fieldTwo
将分别从json中省略,因为它是类注释的默认设置。 fieldThree
however will override the default and will always be included, because of the annotation on the field. 但是,由于字段上的注释, fieldThree
将覆盖默认值并始终包含在内。
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Foo {
private String fieldOne;
private String fieldTwo;
@JsonInclude(JsonInclude.Include.ALWAYS)
private String fieldThree;
}
UPDATE UPDATE
The above is for Jackson 2 . 以上是杰克逊2 。 For earlier versions of Jackson you need to use: 对于早期版本的Jackson,您需要使用:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
instead of 代替
@JsonInclude(JsonInclude.Include.NON_NULL)
If this update is useful, please upvote ZiglioUK's answer below, it pointed out the newer Jackson 2 annotation long before I updated my answer to use it! 如果这个更新很有用,请在下面提到ZiglioUK的答案,它在我更新我的答案之前很久就指出了更新的Jackson 2注释!
#6楼
在Jackson 2.x中,使用:
@JsonInclude(JsonInclude.Include.NON_NULL)