问题:是否可以在创建POJO时覆盖POJO中定义的Include.NON_NULL
解释:
假设我有一个POJO,如下所示:
@JsonInclude(Include.NON_NULL)
class POJO {
String name;
String description;
//Constructors, Getters & Setters, etc
}以及下面的测试类:
class Main {
public static void main(String args[]) {
POJO p = new POJO();
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.setSerializationInclusion(Include.ALWAYS)
.writerWithDefaultPrettyPrinter()
.writeValueAsString(p);
//jsonString should contain empty name & description fields, but they doesn't
}
}发布于 2017-08-08 19:14:27
您可以使用混合方式,因为它比注释具有优先级。
@JsonInclude(JsonInclude.Include.ALWAYS)
public class MixIn {
}然后把它加到地图上
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(POJO.class, MixIn.class);结果将是
{"name":null,"description":null}https://stackoverflow.com/questions/45571979
复制相似问题