如果该字段的值为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;
}
答案 0 :(得分:1010)
要使用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 answer至How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson提供了一个更完整的示例。
答案 1 :(得分:120)
杰克逊> 1.9.11和< 2.x使用@JsonSerialize
注释来执行此操作:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
答案 2 :(得分:110)
只是扩展其他答案 - 如果你需要在每个字段的基础上控制遗漏空值,请注释相关字段(或者注释字段的'getter')。
示例 - 此处只有fieldOne
将从json中省略,如果它为null。无论是否为空,都会包含fieldTwo
。
public class Foo {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String fieldOne;
private String fieldTwo;
}
要将类中的所有空值省略为默认值,请注释该类。如果需要,仍然可以使用每个字段/ getter注释覆盖此默认值。
示例 - 此处fieldOne
和fieldTwo
将分别从json中省略,因为这是类注释的默认设置。但是,fieldThree
将覆盖默认值,并且将始终包括在内,因为该字段上有注释。
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Foo {
private String fieldOne;
private String fieldTwo;
@JsonInclude(JsonInclude.Include.ALWAYS)
private String fieldThree;
}
更新
以上是 Jackson 2 。对于Jackson的早期版本,您需要使用:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
而不是
@JsonInclude(JsonInclude.Include.NON_NULL)
如果此更新有用,请在下面提交ZiglioUK的答案,它在我更新我的答案之前很久就指出了较新的Jackson 2注释!
答案 3 :(得分:57)
在Jackson 2.x中,使用:
@JsonInclude(JsonInclude.Include.NON_NULL)
答案 4 :(得分:35)
您可以使用以下映射器配置:
mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
自2.5以来你可以使用:
mapper.setSerializationInclusion(Include.NON_NULL);
答案 5 :(得分:17)
您可以设置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
答案 6 :(得分:12)
在我的情况下
@JsonInclude(Include.NON_EMPTY)
让它发挥作用。
答案 7 :(得分:7)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
应该有用。
Include.NON_EMPTY
表示如果属性的值不为null且不为空,则序列化该属性。
Include.NON_NULL
表示如果属性值不为null,则序列化该属性。
答案 8 :(得分:5)
如果要将此规则添加到Jackson 2.6+中的所有模型,请使用:
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
答案 9 :(得分:5)
如果在 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
答案 10 :(得分:4)
这将在 Spring boot 2.0.3+和Jackson 2.0 +
中起作用import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiDTO
{
// your class variable and
// methods
}
答案 11 :(得分:3)
对于Jackson 2.5使用:
@JsonInclude(content=Include.NON_NULL)
答案 12 :(得分:2)
这让我困扰了很长一段时间,我终于找到了问题。问题是由于导入错误。早些时候我一直在使用
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)
答案 13 :(得分:2)
使用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;
}
}
答案 14 :(得分:2)
如果您正在尝试序列化对象列表并且其中一个为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。答案 15 :(得分:0)
杰克逊2.x +使用
mapper.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_NULL);
答案 16 :(得分:0)
此外,按照文档中的说明使用Map myVariable来消除空值时,您还必须更改方法:
From documentation:
com.fasterxml.jackson.annotation.JsonInclude
@JacksonAnnotation
@Target(value={ANNOTATION_TYPE, FIELD, METHOD, PARAMETER, TYPE})
@Retention(value=RUNTIME)
Annotation used to indicate when value of the annotated property (when used for a field, method or constructor parameter), or all properties of the annotated class, is to be serialized. Without annotation property values are always included, but by using this annotation one can specify simple exclusion rules to reduce amount of properties to write out.
*Note that the main inclusion criteria (one annotated with value) is checked on Java object level, for the annotated type, and NOT on JSON output -- so even with Include.NON_NULL it is possible that JSON null values are output, if object reference in question is not `null`. An example is java.util.concurrent.atomic.AtomicReference instance constructed to reference null value: such a value would be serialized as JSON null, and not filtered out.
To base inclusion on value of contained value(s), you will typically also need to specify content() annotation; for example, specifying only value as Include.NON_EMPTY for a {link java.util.Map} would exclude Maps with no values, but would include Maps with `null` values. To exclude Map with only `null` value, you would use both annotations like so:
public class Bean {
@JsonInclude(value=Include.NON_EMPTY, content=Include.NON_NULL)
public Map<String,String> entries;
}
Similarly you could Maps that only contain "empty" elements, or "non-default" values (see Include.NON_EMPTY and Include.NON_DEFAULT for more details).
In addition to `Map`s, `content` concept is also supported for referential types (like java.util.concurrent.atomic.AtomicReference). Note that `content` is NOT currently (as of Jackson 2.9) supported for arrays or java.util.Collections, but supported may be added in future versions.
Since:
2.0
答案 17 :(得分:0)
我们对这个问题有很多答案。在某些情况下,此答案可能会有所帮助 如果要忽略空值,则可以在类级别使用NOT_NULL。 如下
@JsonInclude(Include.NON_NULL)
class Foo
{
String bar;
}
有时候您可能需要忽略空值,例如可能已经初始化了arrayList但该列表中没有任何元素。那时候使用NOT_EMPTY批注会忽略那些空值字段
@JsonInclude(Include.NON_EMPTY)
class Foo
{
String bar;
}
答案 18 :(得分:0)
第一种情况
@JsonInclude(JsonInclude.Include.NON_NULL)
private String someString;
第二种情况
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String someString;
如果someString
为null,则在两种情况下均将其忽略。
如果someString
为“”,则仅在情况二中被忽略。
与List = null
或List.size() = 0
相同
答案 19 :(得分:0)
试试这个 -
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class XYZ {
protected String field1;
protected String field2;
}
对于非空值(在 getter/class 级别)-
@JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)