Java Jackson @JsonProperty(required=true) 不会抛出异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18320731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson @JsonProperty(required=true) doesn't throw an exception
提问by jaksky
I am using Hymanson 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?
我正在使用 Hymanson 2.2 注释 @JsonProperty,并将 required 设置为 true。通过 ObjectMapper readValue() 方法反序列化不包含该属性的 json 文件时,不会引发异常。它应该以不同的方式工作还是我错过了什么?
My dto class:
我的 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);
}
}
This is how it is actually called:
这就是它的实际名称:
Serializer serializer = new Serializer();
User result = serializer.readUser(new File("user.json"));
Actuall json looks like:
实际的 json 看起来像:
{"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.
我希望因为 _name 没有在 json 文件中指定并且需要抛出异常。
采纳答案by StaxMan
As per Hymanson 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."
根据 Hymanson 注释javadocs:“请注意,从 2.0 开始,BeanDeserializer 不使用此属性:预计会为以后的次要版本添加支持。”
That is: no validation is performed using this settings. It is only (currently) used for generating JSON Schema, or by custom code.
即:不使用此设置执行验证。它仅(当前)用于生成 JSON Schema,或用于自定义代码。
回答by Bojan Petkovic
With Hymanson 2.6 you can use required, however you have to do it using JsonCreator
使用 Hymanson 2.6,您可以使用 required,但是您必须使用 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.
如果 x 或 y 不存在,则在尝试反序列化时会抛出异常。