2017-03-10 5 views
0

Ich habe ein Frühlingsprojekt und mit Jackson für JS Serialisierung und Deserialisierung.Validieren verschachtelten Objekt in Jackson

Und ich habe diese DTOs definiert,

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 
} 

Sie können sehen, über dem habe ich gesetzt habe ich @NotNull Annotation Set für die Felder certificate & dnzZone.

Allerdings scheint dieses JSON erfolgreich zu parsen. Dies geschieht nur bei verschachtelten Objekten.

{ 
    "orgId":"1001", 
    "certificateType":"Single Use", 
    "privateCertificate":{ 
     "pkcs12":"test", 
     "certificatePassword":"Test" 
    }, 
    "publicCertificate":{ 

    } 
} 

Wenn ich die Post folgende es wird die Validierung nicht als @NotNull für publicCertificate

{ 
    "orgId":"1001", 
    "certificateType":"Single Use", 
    "privateCertificate":{ 
     "pkcs12":"test", 
     "certificatePassword":"Test" 
    } 
} 

I @Valid Satz für die @RequestBody im RestController haben festgelegt ist.

Irgendeine Idee, was könnte das verursachen ??

Antwort

1

Es scheint, Sie @Valid Anmerkung zu Ihrem verschachtelten DTOs hinzuzufügen vergessen. Versuchen Sie den folgenden Code:

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

Sobald ich die Frage geschrieben habe, bin ich auf blog post über Bean-Validierung gestoßen. Sie müssen @Valid, um die verschachtelten Objekte zu überprüfen.

Refactoring Daher CertManDTO wie

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 
} 
Verwandte Themen