[英]Jackson @JsonProperty(required=true) doesn't throw an exception
[英]Validating (required=true) set in JsonProperty while using ObjectMapper in Jackson should throw Exception
我正在嘗試使用 Jackson 庫進行反序列化,因為我有一個場景,如果它設置為required=true
,我必須驗證許多 JsonProperty 中的null
值。
這是代碼片段。
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
類。 如果JsonProperty
的required
屬性設置為true
並且反序列化時如果此屬性遇到null
(對於上述代碼中的id
和name
字段),我必須拋出自定義DeserializationException
。
而且我還需要為在 JsonProperty(對於age
字段)中設置required=false
字段處理null
值(不應該失敗)。
在這里我不能使用MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL)
因為我需要處理required=false
字段的null
值。
請讓我知道如何使用ObjectMapper.readValue
方法或任何其他適用於此的方法來實現這一點。
任何建議,將不勝感激。
您不能使用@JsonProperty
進行空驗證。 它只檢查值是否存在。 來自javadocs
指示在反序列化期間是否需要屬性的值(可能是顯式 null)的屬性。
對於空驗證,您可以使用 Bean 驗證 JSR-380。 休眠示例:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.