2017-03-10 31 views
0

我有一个春季项目,并使用杰克森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组将失败的验证。

任何想法可能会导致这?

回答

1

看来你忘了@Valid注释添加到您的嵌套的DTO。请尝试以下代码:

@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) 
@NotNull 
@Valid 
private PublicCertificateDTO publicCertificate; 
0

只要我发布了这个问题,我碰到了关于bean验证的blog post。你需要@Valid来验证嵌套的对象。

因此重构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 
    @Valid 
    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 
}