我正在尝试使用 Jackson 库进行反序列化,因为我有一个场景,其中我必须验证 null
许多 JsonProperty 中的值(如果设置为 required=true
) 。
这是代码片段。
public class JacksonValidator {
private static final ObjectMapper MAPPER = new ObjectMapper();
public static void main(String[] args) {
// Should succeed since all properties have value and required=true holds good
validate("{\"id\": \"1\",\"age\": 26,\"name\": \"name1\"}");
// Should throw exception since name is null (because of required=true)
validate("{\"id\": \"2\",\"age\": 28,\"name\": null}");
// Should throw exception since id is null (because of required=true)
validate("{\"id\": null,\"age\": 27,\"name\": \"name2\"}");
}
public static void validate(String json) {
try {
Customer customer = MAPPER.readValue(json, Customer.class);
System.out.println(customer);
}
catch (IOException e) {
throw new DeserializationException(String.format("Validation failed. Unable to parse json %s", json), e);
}
}
@Setter
@Getter
@ToString
public static class Customer {
private String id;
private Integer age;
private String name;
@JsonCreator
public Customer(@JsonProperty(value = "id", required = true) String id,
@JsonProperty(value = "age", required = false) Integer age,
@JsonProperty(value = "name", required = true) String name) {
this.id = id;
this.age = age;
this.name = name;
}
}
}
正如您在上面的代码中看到的,我正在尝试反序列化 JSON
进入Customer
类(class)。如果required
属性设置为true
对于JsonProperty
并且在反序列化时如果此属性遇到 null
(对于上面代码中的 id
和 name
字段),我必须抛出一个自定义 DeserializationException
.
而且我还需要null
对于 required=false
的字段处理值(不应失败)在 JsonProperty 中设置(对于 age
字段)。
这里我不能使用MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL)
因为我需要处理 null
required=false
的值字段。
请告诉我如何使用 ObjectMapper.readValue
来实现这一点方法或任何其他在这里适用的方法。
如有任何建议,我们将不胜感激。
最佳答案
您不能使用@JsonProperty
进行 null 验证。它仅检查值是否存在。来自 javadocs
Property that indicates whether a value (which may be explicit null) is expected for property during deserialization or not.
对于 null 验证,您可以使用 Bean 验证 JSR-380。 hibernate 示例:
Maven 依赖项:
<!-- Java bean validation API - Spec -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!-- Hibernate validator - Bean validation API Implementation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.11.Final</version>
</dependency>
<!-- Verify validation annotations usage at compile time -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.0.11.Final</version>
</dependency>
<!-- Unified Expression Language - Spec -->
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.1-b06</version>
</dependency>
<!-- Unified Expression Language - Implementation -->
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
</dependency>
然后你可以像这样使用它:
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<Customer>> constraintViolations = validator.validate(customer);
if (constraintViolations.size() > 0) {
throw new DeserializationException(String.format("Validation failed. Unable to parse json %s", json), e);
}
关于java - 在 Jackson 中使用 ObjectMapper 时验证 JsonProperty 中设置的 (required=true) 应该抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53948026/