java – @Deprecated SerializationFeature.WRITE_EMPTY_JSON_ARRAYS的替代方案

我在查找在SerializationFeature.WRITE_EMPTY_JSON_ARRAYS上修复弃用警告的正确方法时遇到了困难.

Javadocs说明了这一点

Since 2.8 there are better mechanism for specifying filtering;
specifically using com.fasterxml.jackson.annotation.JsonFormat or
configuration overrides.

但我会假设

ObjectMapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);

是一个配置覆盖,虽然上面的行触发了弃用警告.

有哪些其他替代品不会污染带有另一个注释的模型类?我想全局配置行为.

最佳答案
在类级别,您可以使用@JsonInclude,如:

@JsonInclude( JsonInclude.Include.NON_EMPTY )
public class MyClass ...

此外,在映射器级别,您可以执行以下操作:

mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

转载注明原文:java – @Deprecated SerializationFeature.WRITE_EMPTY_JSON_ARRAYS的替代方案 - 代码日志