如果jackson在序列化过程中忽略某个字段的值是否为空,该怎么办?
jackson如何configuration为在序列化期间忽略字段值,如果该字段的值为空。
例如:
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; }  要使用空值来压缩序列化属性,可以直接configurationObjectMapper ,或者使用@JsonInclude注解: 
 mapper.setSerializationInclusion(Include.NON_NULL); 
要么:
 @JsonInclude(Include.NON_NULL) class Foo { String bar; } 
 或者,您可以在getter中使用@JsonInclude ,以便如果值不为null,则会显示该属性。 
一个更完整的例子可以在我的回答 如何防止在一个Map中的空值和在一个bean中的空字段通过Jackson进行序列化 。
 随着jackson> 1.9.11和<2.x使用@JsonSerialize注释来做到这一点: 
 @JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) 
只是为了扩大其他答案 – 如果你需要在每个字段的基础上控制空值的省略,注释有问题的字段(或者注释字段的“getter”)。
  例如 –这里只有fieldOne将被从json中省略,如果它为null。  fieldTwo将始终包含在内,无论它是否为null。 
 public class Foo { @JsonInclude(JsonInclude.Include.NON_NULL) private String fieldOne; private String fieldTwo; } 
要省略类中的所有空值作为默认值,请注释该类。 如果有必要的话,仍然可以使用per-field / getter注释覆盖这个默认值。
  例如 –这里fieldOne和fieldTwo将分别从null,因为这是默认的类注释设置将被省略。  fieldThree将覆盖默认值,并且将始终包含在内,因为字段上的注释。 
 @JsonInclude(JsonInclude.Include.NON_NULL) public class Foo { private String fieldOne; private String fieldTwo; @JsonInclude(JsonInclude.Include.NON_NULL) private String fieldThree; } 
更新 使用jackson2 – 早期版本的jackson使用
 @JsonSerialize(include=JsonSerialize.Inclusion.ALWAYS) 
代替
 @JsonInclude(JsonInclude.Include.NON_NULL) 
如果这个更新是有用的,请在下面提出ZiglioUK的答案,它在这个更新之前就指出了这个更新的注释!
在Jackson 2.x中,使用:
 @JsonInclude(JsonInclude.Include.NON_NULL) 
您可以使用以下映射器configuration:
 mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL); 
由于2.5你可以用户:
 mapper.setSerializationInclusion(Include.NON_NULL); 
在我的情况
 @JsonInclude(Include.NON_EMPTY) 
使其工作。
 @JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_EMPTY) 
应该工作。
  Include.NON_EMPTY表示属性被序列化,如果它的值不为空而不是空的。  Include.NON_NULL表示属性被序列化,如果它的值不为空。 
如果你想添加这个规则到jackson2.6+的所有模型使用:
 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); 
对于jackson2.5使用:
 @JsonInclude(content=Include.NON_NULL) 
你可以设置application.properties(或application.yml或其他):
jackson.default属性模型 – 包裹体= non_null
http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
这一直困扰着我很长一段时间,我终于find了问题。 这个问题是由于一个错误的import。 早些时候我一直在使用
 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) 
jackson2.x +使用
 mapper.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_NULL); 
- Spring 4.2.3和fasterxml Jackson 2.7.0是不兼容的
- 如何防止一个Map中的空值和一个Bean中的空字段通过Jackson序列化
- 序列化/反序列化java 8jacksonJSON映射器的java.time
- 在序列化Hibernate对象时引发奇怪的jacksonexception
- 将JSONstring转换为使用Jackson的漂亮打印JSON输出
- 使用Jackson ObjectMapper和Java 8可选值
- jacksonJson并没有这样的方法错误
- 使用Jackson更改JSON中的字段名称
- org.codehaus.jackson与com.fasterxml.jackson.core