简体   繁体   English

如果没有字段,Jackson将无法进行反序列化

[英]Jackson fail deserialization if field is not present

I have a pojo like 我有一个pojo喜欢

class Pojo {
    private String string;

    public String getString() { return string; }

    @JsonSetter(nulls = Nulls.FAIL)
    public void setString(String string) {
        this.string = string;
    }
}

I want to make jackson fail when deserializing if the string field is null or absent. 如果string字段为null或不存在,我想让杰克逊在反序列化时失败。 (ie {"string":null} or {} ) (即{"string":null}{}

As you can see, I've succeeded in the first goal with the JsonSetter annotation. 如您所见,我通过JsonSetter注释成功实现了第一个目标。 What I am hoping for now is something like that but for a missing property. 我现在希望的是那样的东西,但缺少财产。 I found a few other questions asking similar things but they were quite old and referenced features that might be implemented in the future. 我发现了其他一些类似的问题,但是它们都是很老的参考功能,将来可能会实现。 With the recent release of jackson 2.9, I was hoping maybe this is now possible. 在杰克逊2.9的最新发行版中,我希望现在可能做到这一点。

@JsonProperty has a required element that can be used @JsonProperty具有可以使用的required元素

to ensure existence of property value in JSON 确保JSON中存在属性值

Unfortunately, Jackson currently (2.9) only supports it for use with @JsonCreator annotated constructors or factory methods. 不幸的是,杰克逊目前(2.9)仅支持将其与@JsonCreator注释的构造函数或工厂方法一起使用。 Since @JsonSetter only works with setters, you'll have to do the null validation yourself. 由于@JsonSetter仅适用于setter,因此您必须自己进行null验证。

For example, you'd define a constructor like 例如,您将定义一个类似

@JsonCreator
public Pojo(@JsonProperty(value = "string", required = true) String string) {
    if (string == null) {
        throw new IllegalArgumentException("string cannot be null");
    }
    this.string = string;
}

If the property is present, but set to null , Jackson would throw an InvalidDefinitionException that wraps the IllegalArgumentException thrown in the constructor. 如果存在该属性,但将其设置为null ,则Jackson将引发InvalidDefinitionException ,该异常将包装在构造函数中引发的IllegalArgumentException包装起来。

If the property is absent, Jackson would throw a MismatchedInputException stating that a property is missing. 如果该属性不存在,Jackson将抛出MismatchedInputException指出缺少该属性。

Both of these exceptions are subtypes of JsonMappingException , so you can easily deal with them the same way. 这两个异常都是JsonMappingException子类型,因此您可以轻松地以相同的方式处理它们。


With this solution, you could also get rid of the setter altogether and make the field final if that suited your design better. 使用此解决方案,您也可以完全摆脱设置者的烦恼,并在更适合您的设计的情况下final确定领域。

You may perform bean validation here by annotating the field of interest with @NotNull . 您可以在此处通过使用@NotNull注释感兴趣的字段来执行bean验证。 You may remove the annotation from your setter. 您可以从设置者中删除注释。

class Pojo {

    @NotNull
    private String string;

    public String getString() { return string; }

    public void setString(String string) {
        this.string = string;
    }

}

Similarly if you want to fail the validation for other constraints like size, pattern etc, you may use similar equivalent annotations available here . 同样,如果您想使其他约束(如大小,图案等)的验证失败,则可以使用此处提供的类似等效注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM