如果字段的值为空,那么如何将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;
}
如果字段的值为空,那么如何将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;
}
当前回答
如果在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-自定义jackson对象映射器
其他回答
这已经困扰了我一段时间,我终于找到了问题所在。问题是由于错误的导入。早些时候我一直在使用
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)
如果使用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;
}
}
此外,当使用MapmyVariable(如文档中所述)来命名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
只是为了扩展其他答案-如果您需要控制每个字段中空值的省略,请注释有问题的字段(或者注释字段的“getter”)。
示例-这里只有fieldOne如果为空,将从JSON中省略。fieldTwo将始终包含在JSON中,无论它是否为空。
public class Foo {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String fieldOne;
private String fieldTwo;
}
要忽略类中的所有空值作为默认值,请注释该类。如果需要,Per-field/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;
}
更新
以上是《杰克逊2》。对于早期版本的Jackson,您需要使用:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
而不是
@JsonInclude(JsonInclude.Include.NON_NULL)
如果这个更新有用,请在下面投票支持ZiglioUK的答案,它早在我更新答案使用它之前就指出了更新的Jackson 2注释!
对于Jackson 2.5使用:
@JsonInclude(content=Include.NON_NULL)