您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何告诉Jackson在反序列化期间忽略空对象?

如何告诉Jackson在反序列化期间忽略空对象?

我会用一个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

 }

希望这可以帮助!

其他 2022/1/1 18:24:57 有508人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶