2013-08-19 127 views
25

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

我的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; 
    } 
} 

这是我该如何反序列化类:

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的样子:

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

我认为,因为_name没有在json文件中指定,并且需要抛出异常。

回答

15

根据Jackson注释javadocs:“请注意,从2.0开始,BeanDeserializer不使用此属性:预计将为稍后的次版本添加支持。”

即:使用此设置不执行验证。它只是(当前)用于生成JSON模式或通过自定义代码。

+0

什么是生成模式v.4出于验证目的的最佳方式? – jaksky

+0

ObjectMapper mapper = new ObjectMapper(); JsonSchema schema = mapper.generateJsonSchema(User.class); 当我通过这种方式生成架构时,它在验证无效架构期间声明 – jaksky

+0

您可能想要就此提出一个单独的问题。与杰克逊,你会使用外部模块(https://github.com/FasterXML/jackson-module-jsonSchema)。但还有其他的JSON Schema生成器;我不用任何东西来使用JSON Schema,所以我不能评论哪一个最好。 – StaxMan

10

与杰克逊2.6,您可以使用所需的,但是你必须使用JsonCreator

例如,要做到这一点:

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

如果x或y是不存在的异常会尝试反序列化时抛出它。

+1

但是(对于杰克逊2.6.5无论如何),如果你的JSON将x赋值为“null”,那么Jackson会高兴地接受这个为有效的。 – fragorl

-3

使用@NotNull代替@JsonProperty。它工作100%并抛出MethodArgumentNotValidException。

@NotNull 
    @JsonProperty("property") 
    private String prop; 

Here是更多选项的链接。

+5

它不适用于我的情况 –