60

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.

jaksky
  • 3,305
  • 4
  • 35
  • 68

2 Answers2

36

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.

tom
  • 21,844
  • 6
  • 43
  • 36
Bojan Petkovic
  • 2,406
  • 15
  • 26
  • 9
    However (for Jackson 2.6.5 at any rate), if your JSON assigns x to "null" then Jackson will happily accept this as valid. – fragorl Sep 19 '17 at 06:10
  • 3
    @fragorl I think that's intended behavior. The point isn't to say that it has to be non-null, just that it has to be _provided_. If you provide it, but you provide null, that's being given. Then you'd need to do more validation to make sure you don't have invalid data (e.g. nulls) – Nic Nov 04 '18 at 05:13
  • @fragorl `rquired` should be `appeared` more appropriate – Tiina Apr 13 '22 at 01:08
32

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.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • 1
    What is the best way to generate a schema v.4 for validation purposes from that? – jaksky Aug 20 '13 at 12:23
  • ObjectMapper mapper = new ObjectMapper(); JsonSchema schema = mapper.generateJsonSchema(User.class); When I generate the schema this way it claims during the validation invalid schema – jaksky Aug 20 '13 at 12:29
  • 1
    You may want to ask a separate question on that. With Jackson, you would use external module (https://github.com/FasterXML/jackson-module-jsonSchema). But there are other JSON Schema generators; I don't use JSON Schema myself for anything so I can't comment on which one is best. – StaxMan Aug 20 '13 at 17:40