Jackson @JsonProperty(required=true) 不会抛出异常 [英] Jackson @JsonProperty(required=true) doesn't throw an exception

查看:24
本文介绍了Jackson @JsonProperty(required=true) 不会抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jackson 2.2 注释 @JsonProperty,并将 required 设置为 true.通过 ObjectMapper readValue() 方法反序列化不包含该属性的 json 文件时,不会引发异常.它应该以不同的方式工作还是我错过了什么?

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() method no exception is being thrown. Is it supposed to work in a different way or did I missed something?

我的 dto 类:

public class User {
    public enum Gender {MALE, FEMALE}

    ;

    public static class Name {
        private String _first, _last;

        public String getFirst() {
            return _first;
        }

        public String getLast() {
            return _last;
        }

        public void setFirst(String s) {
            _first = s;
        }

        public void setLast(String s) {
            _last = s;
        }
    }

    private Gender _gender;
    private Name _name;
    private boolean _isVerified;
    private byte[] _userImage;

    @JsonProperty(value ="NAAME",required = true)
    public Name getName() {
        return _name;
    }

    @JsonProperty("VERIFIED")
    public boolean isVerified() {
        return _isVerified;
    }

    @JsonProperty("GENDER")
    public Gender getGender() {
        return _gender;
    }
    @JsonProperty("IMG")
    public byte[] getUserImage() {
        return _userImage;
    }

    @JsonProperty(value ="NAAME",required = true)
    public void setName(Name n) {
        _name = n;
    }
    @JsonProperty("VERIFIED")
    public void setVerified(boolean b) {
        _isVerified = b;
    }
    @JsonProperty("GENDER")
    public void setGender(Gender g) {
        _gender = g;
    }
    @JsonProperty("IMG")
    public void setUserImage(byte[] b) {
        _userImage = b;
    }
}

这是我如何反序列化类:

This is how do I deserialize the class:

public class Serializer {
    private ObjectMapper mapper;

    public Serializer() {
        mapper = new ObjectMapper();
        SimpleModule sm = new SimpleModule("PIF deserialization");
        mapper.registerModule(sm);
    }

    public void writeUser(File filename, User user) throws IOException {
        mapper.writeValue(filename, user);
    }

    public User readUser(File filename) throws IOException {
          return mapper.readValue(filename, User.class);
      }
}

实际上是这样称呼的:

    Serializer serializer = new Serializer();
    User result = serializer.readUser(new File("user.json"));

实际的json看起来像:

Actuall json looks like:

{"GENDER":"FEMALE","VERIFIED":true,"IMG":"AQ8="}

我希望因为 _name 未在 json 文件中指定,并且需要抛出异常.

I would expect that since _name is not specified in json file and is required that the exception will be thrown.

推荐答案

Asper Jackson annotations javadocs:请注意,从 2.0 开始,BeanDeserializer 不使用此属性:预计会为更高的次要版本添加支持."

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."

即:不使用此设置执行验证.它仅(当前)用于生成 JSON Schema,或用于自定义代码.

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

这篇关于Jackson @JsonProperty(required=true) 不会抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆