Jackson @JsonProperty(required=true) doesn't throw an exception

后端 未结 2 2230
孤街浪徒
孤街浪徒 2020-11-29 09:05

I am using jackson 2.2 annotation @JsonProperty with required set to true. While deserializing json file which doesn\'t contain that property via ObjectMapper readValue() me

相关标签:
2条回答
  • 2020-11-29 09:30

    With Jackson 2.6 you can use required, however you have to do it using JsonCreator

    For example:

    public class MyClass {
    
        @JsonCreator
        public MyClass(@JsonProperty(value = "x", required = true) Integer x, @JsonProperty(value = "value_y", required = true) Integer y) {
            this.x = x;
            this.y = y;
        }
    
        private Integer x;
        private Integer y;
    }
    

    If x or y are not present an exception will be thrown when trying to deserialize it.

    0 讨论(0)
  • 2020-11-29 09:31

    As per Jackson annotations javadocs: "Note that as of 2.0, this property is NOT used by BeanDeserializer: support is expected to be added for a later minor version."

    That is: no validation is performed using this settings. It is only (currently) used for generating JSON Schema, or by custom code.

    0 讨论(0)
提交回复
热议问题