您将注解放置在错误的位置-注解必须在类上,而不是在字段上。即:
@JsonInclude(Include.NON_NULL) //or Include.NON_EMPTY, if that fits your use case public static class Request { // ...}如注释中所述,在2.x以下的版本中,此注释的语法为:
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) // or JsonSerialize.Inclusion.NON_EMPTY
另一种选择是
ObjectMapper直接配置,只需调用
mapper.setSerializationInclusion(Include.NON_NULL);
(为了便于记录,我认为此答案的流行程度表明该注释 应 在逐个字段的基础上应用@fasterxml)



