2017-03-10 1 views
0

Ive는 json 직렬화 및 직렬화 복원을 위해 spring 프로젝트를 사용하고 jackson을 사용합니다.잭슨에서 중첩 된 오브젝트 유효성 확인

그리고 나는이 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가 나는 RestController@RequestBody에 대한 @Valid 설정을해야합니까 publicCertificate

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

설정되어대로 검증을 실패합니다 다음.

어떤 생각이 원인 일 수 있습니까 ??

답변

1

로 CertManDTO를 리팩토링. 다음 코드를 사용해보십시오.

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

질문을 올리 자마자 난 콩 인증에 대해이 blog post을 발견했습니다. 중첩 된 객체의 유효성을 검사하려면 @Valid이 필요합니다.

은 따라서 당신이 당신의 중첩 된 DTO들에 @Valid 주석을 추가하는 것을 잊었다 보인다

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 
}