[英]jackson json parser accepting empty single quotes for int primitives
I have defined a REST element in our project which contains attributes with primitive int & long . 我在项目中定义了一个REST元素,其中包含具有原始int&long的属性。 I am using Jersey 2.17 During testing when I tried to pass a "" string, the REST client did not report any validation errors, but in the rest resource, I get the value of the primitive as the default value 0. This behavior is very odd as an empty string should not be accepted in this case. 我使用的是Jersey 2.17,在测试中尝试传递“”字符串时,REST客户端未报告任何验证错误,但是在其余资源中,我将原语的值设置为默认值0。此行为非常奇数,因为在这种情况下不应接受空字符串。 How do I make the request fail with validation errors. 如何使请求失败并出现验证错误。
My JSON request:
JSONObject employee = new JSONObject("{name : James}");
rateLimiter.put("age", "");
Response :
POST -><http://134.141.206.113:8080/Employee >
data: {"name":"James","age":""}
Status :201 output: {
"name" : "James",
"age" : 0
}
One way to check the payload's field for emptiness or null is by using the Jackson's annotations to skip these fields while deserializing. 检查有效载荷字段是否为空或为空的一种方法是使用杰克逊的注释在反序列化时跳过这些字段。 In this way, if a compulsory field is not present in the deserialized object then you can assume that the field was empty while the request was made. 这样,如果反序列化对象中不存在必填字段,则可以假定在发出请求时该字段为空。
Jackson's Annotations 杰克逊的注释
Add them on top where the fields are declared in the DTO/entity class 将它们添加到DTO / entity类中声明字段的顶部
@JsonInclude(Include.NON_DEFAULT)
private int age = 0;
@JsonInclude(Include.NON_NULL)
private String name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.