我有一个春季项目,并使用杰克森json序列化和反序列化。在杰克逊验证嵌套的对象
而且我已经定义了这些DTO的,
CertManDTO,
public class CertManDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private UUID certificateId;
@NotNull
private Long orgId;
@NotNull
private CertificateType certificateType;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@NotNull
private PublicCertificateDTO publicCertificate;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private PrivateCertificateDTO privateCertificate;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private ZonedDateTime expiryDate;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private ZonedDateTime createdDate;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private ZonedDateTime updatedDate;
// Getters and setters
}
PublicCertificateDTO,
public class PublicCertificateDTO implements Serializable {
private static final long serialVersionUID = 1L;
@JsonIgnore
private Long id;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@NotNull
private String certificate;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@NotNull
private String dnsZone;
// Getters and setters
}
PrivateCertificateDTO,
public class PrivateCertificateDTO implements Serializable {
private static final long serialVersionUID = 1L;
@JsonIgnore
private Long id;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String pkcs12;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String privateCertificatePassword;
// Getters and setters
}
你可以看到在上面我已经设置了我有@NotNull
注释集的字段certificate
& dnzZone
。
但发布此JSON似乎解析成功。这只发生在嵌套对象上。
{
"orgId":"1001",
"certificateType":"Single Use",
"privateCertificate":{
"pkcs12":"test",
"certificatePassword":"Test"
},
"publicCertificate":{
}
}
如果我发布了以下的@NotNull
设置为publicCertificate
{
"orgId":"1001",
"certificateType":"Single Use",
"privateCertificate":{
"pkcs12":"test",
"certificatePassword":"Test"
}
}
我必须在RestController
的@RequestBody
@Valid
组将失败的验证。
任何想法可能会导致这?