Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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?

My dto class:

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);
      }
}

This is how it is actually called:

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

Actuall json looks like:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...