Java Jackson的必填但可为空的字段

Java Jackson的必填但可为空的字段,java,json,jackson,Java,Json,Jackson,我正在使用Jackson为JSON文件实现一个检测器反序列化器 这些JSON文件可以包含空值。但是,对于检测阶段,我希望jackson能够区分字段是否存在但为空,或者根本不存在 例如: 我有一个车辆Java类: class车辆{ //必填字段 私有字符串id; 自有品牌; 私有字符串模型; //可选字段 私有字符串颜色; 私人双倍长度; 私人双宽度; } 我希望成功解析这些JSON { "id": 123456, "brand": "Ford", "

我正在使用Jackson为JSON文件实现一个检测器反序列化器

这些JSON文件可以包含空值。但是,对于检测阶段,我希望jackson能够区分字段是否存在但为空,或者根本不存在

例如: 我有一个车辆Java类:

class车辆{
//必填字段
私有字符串id;
自有品牌;
私有字符串模型;
//可选字段
私有字符串颜色;
私人双倍长度;
私人双宽度;
}
我希望成功解析这些JSON

    {
      "id": 123456,
      "brand": "Ford",
      "model": null, // field is present (null, but present)

      "color": "white",
      "length": 410,
      "width": 230,
    }

    {
      "id": 123456,
      "brand": "Ford",
      "model": null, // field is present (null, but present)

      "color": "white", // missing optional fields, but they are optional
    }

    {
      "id": 123456,
      "brand": "Ford",
      "model": "FordModel",

      "color": "white",
      "length": 410,
      "width": 230,
      "height": 210 // additional field not in Java class
    }
但是,我希望根本不要解析以下JSON(jackson抛出异常很好):

我找到了一种忽略空值的方法(通过使用可选或使用Jackson注释),但问题是我找不到一种方法来区分“车辆字段存在但为空”和“车辆字段根本不存在”


实现这一点的最佳方法是什么?

Jackson可能会因为缺少json creator属性而失败

根据您想要实现的自定义级别,可以通过多种方式实现

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, true);
然后添加一个
@JsonCreator
构造函数,该构造函数包含在缺少属性时应该失败的所有属性

    public class Vehicle {
        private Long id;
        private String brand;
        private String model;

        private String color;
        private double length;
        private double width;

        @JsonCreator
        public Vehicle(@JsonProperty(value = "id", required = true) Long id,
                       @JsonProperty(value = "brand", required = true) String brand,
                       @JsonProperty(value = "model", required = true) String model) {
            this.id = id;
            this.brand = brand;
            this.model = model;
        }

    // getters and setters
    }

Jackson可能因缺少json创建者属性而失败

根据您想要实现的自定义级别,可以通过多种方式实现

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, true);
然后添加一个
@JsonCreator
构造函数,该构造函数包含在缺少属性时应该失败的所有属性

    public class Vehicle {
        private Long id;
        private String brand;
        private String model;

        private String color;
        private double length;
        private double width;

        @JsonCreator
        public Vehicle(@JsonProperty(value = "id", required = true) Long id,
                       @JsonProperty(value = "brand", required = true) String brand,
                       @JsonProperty(value = "model", required = true) String model) {
            this.id = id;
            this.brand = brand;
            this.model = model;
        }

    // getters and setters
    }

您使用的是哪种注释?发布代码我已经添加了一些细节,希望能有所帮助。现在,我很天真地使用了JsonIgnore注释,但到目前为止还没有取得很大的成功。您使用的是哪种注释?发布代码我已经添加了一些细节,希望能有所帮助。现在,我很天真地使用了JsonIgnore注释,但到目前为止还没有取得很大的成功。嗯,我仍然无法实现我想要做的事情。我添加了一个更具体的例子,可能不是很清楚。嗯,我仍然无法实现我想要做的。我添加了一个更具体的例子,可能不是很清楚。