Answers:
要使用Jackson> 2.0抑制具有空值的序列化属性,可以直接配置ObjectMapper
或使用@JsonInclude
批注:
mapper.setSerializationInclusion(Include.NON_NULL);
要么:
@JsonInclude(Include.NON_NULL)
class Foo
{
String bar;
}
或者,您可以@JsonInclude
在getter中使用,以便在值不为null时显示属性。
我对如何防止Map内的空值和bean内的空字段无法通过Jackson序列化的回答提供了一个更完整的示例。
@JsonInclude
表示法不起作用,但这就像一个魅力:(@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
我将杰克逊1.9.12和Spring 3.2.2结合使用)
@JsonSerialize
语法更改为@JsonInclude
。不建议使用较旧的语法。
对于Jackson> 1.9.11和<2.x,请使用@JsonSerialize
注释来做到这一点:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonSerialize(using = FooSerializer.class, include = JsonSerialize.Inclusion.NON_NULL)
不起作用。可空值被序列化。
只是为了扩展其他答案-如果您需要基于每个字段控制空值的省略,请为有问题的字段添加注释(或为字段的“ getter”添加注释)。
例如-这里只fieldOne
从JSON被ommitted如果它是空的。fieldTwo
不论是否为null,都将始终包含在内。
public class Foo {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String fieldOne;
private String fieldTwo;
}
要忽略该类中的所有空值作为默认值,请为该类添加注释。如有必要,仍可以使用按字段/获取器注释来覆盖此默认值。
示例-在此处fieldOne
和fieldTwo
如果它们分别为null,则将从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批注,而我要更新答案才能使用它!
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
已弃用
在Jackson 2.x中,使用:
@JsonInclude(JsonInclude.Include.NON_NULL)
com.fasterxml.jackson.annotation
-杰克逊1.x版本org.codehaus.jackson.annoation
您可以使用以下映射器配置:
mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
从2.5开始,您可以使用:
mapper.setSerializationInclusion(Include.NON_NULL);
getSerializationConfig()
说:Note that since instances are immutable, you can NOT change settings by accessing an instance and calling methods: this will simply create new instance of config object.
mapper.setSerializationInclusion(Include.NON_NULL);
您可以设置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
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
应该管用。
Include.NON_EMPTY
指示如果属性的值不为null且不为空,则该属性被序列化。
Include.NON_NULL
指示属性的值不为null时,将对其进行序列化。
JsonInclude.Include.NON_EMPTY
-足够了,因为它涵盖了NOT_NULL情况
如果要将此规则添加到Jackson 2.6+中的所有模型中,请使用:
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
如果在Spring Boot中,则可以自定义杰克逊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
这将在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
}
@JsonInclude(JsonInclude.Include.NON_NULL)
对于Spring Boot 2.1.2和Jackson注释2.9.0
如果使用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;
}
}
public Jackson2ObjectMapperBuilder serializationInclusion(JsonInclude.Include serializationInclusion) { this.serializationInclusion = serializationInclusion; return this; }
; 应当使用更大的包含枚举半径。例如,NON_ABSENT包含NON_NULL,NON_EMPTY包含两者。因此,只应该是builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
JacksonInclude DOC
如果您要序列化对象列表,并且其中一个为null,则最终会在JSON中包含null项目,即使
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
将导致:
[{myObject},null]
得到这个:
[{myObject}]
一个人可以做类似的事情:
mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object obj, JsonGenerator jsonGen, SerializerProvider unused)
throws IOException
{
//IGNORES NULL VALUES!
}
});
提示:如果您使用的是DropWizard,则可以ObjectMapper
使用environment.getObjectMapper()
我们对这个问题有很多答案。在某些情况下,此答案可能会有所帮助。如果您想忽略空值,则可以在类级别使用NOT_NULL。如下
@JsonInclude(Include.NON_NULL)
class Foo
{
String bar;
}
有时您可能需要忽略空值,例如您可能已经初始化了arrayList但该列表中没有任何元素。
@JsonInclude(Include.NON_EMPTY)
class Foo
{
String bar;
}
Jackson 2.x +使用
mapper.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_NULL);
.withSerializationInclusion(JsonInclude.Include.NON_NULL)
相反吧?
getSerializationConfig()
说:Note that since instances are immutable, you can NOT change settings by accessing an instance and calling methods: this will simply create new instance of config object.
此外,在使用Map myVariable时,还必须更改文档中所述的方法以消除null:
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
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
; 不知何故您的注释不可用。