我会用一个JsonDeserializer
。检查相关字段,确定是否为该字段emtpy
并返回null
,因此你ContainedObject
将为null。
这样的东西(semi-pseudo):
public class MyDes extends JsonDeserializer<ContainedObject> {
@Override
public String deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
//read the JsonNode and determine if it is empty JSON object
//and if so return null
if (node is empty....) {
return null;
}
return node;
}
}
然后在你的模型中:
public class Entity {
private long id;
private String description;
@JsonDeserialize(using = MyDes.class)
private ContainedObject containedObject;
//Contructor, getters and setters omitted
}
希望这可以帮助!