1
私は春のRESTアプリケーションを持っています。単純な@NotNullチェックのために検証できるBeanフィールドを装飾するために@Validアノテーションを使用したいと思います。@Validと@InitBinderを併用した春休み検証
public ResponseEntity<ExtAuthInquiryResponse> performExtAuthInq(@Valid @RequestBody ExtAuthInquiryRequest extAuthInquiryRequest)
私は(1つのフィールドの値に基づいて、第2のフィールドが必須であるように)、より複雑な検証を行うため@initBinder使用することに加えて、この
@NotBlank(message = "requestUniqueId cannot be blank..")
private String requestUniqueId;
よう
@InitBinder("extAuthInquiryRequest")
protected void initExtAuthInqRequestBinder(WebDataBinder binder) {
binder.setValidator(extAuthInqValidator);
}
バリデータの実装は次のとおりです(条件付き検証の場合のみ)
@Override
public void validate(Object target, Errors e) {
ExtAuthInquiryRequest p = (ExtAuthInquiryRequest) target;
// Dont want to do this check here. Can be simply done in the bean using @NotNull checks
ValidationUtils.rejectIfEmpty(e, "requestUniqueId", "requestUniqueId is empty");
// this is a good candidate to be validated here
if(StringUtils.isNotBlank(p.getPersonInfo().getContactInfo().getPhoneNumber().getPhoneType())){
if(StringUtils.isBlank(p.getPersonInfo().getContactInfo().getPhoneNumber().getPhoneNumber())){
e.rejectValue("personInfo.contactInfo.phoneNumber.phoneNumber", "phoneNumber is mandatory when phoneType is provided");
}
}
}
}
私は、どちらか一方を使用するオンラインの例を見てきました。私は両方のアプローチを一緒に実行しようとしましたが、@initBinderの設定があると、リクエストオブジェクトの@valid注釈はそれ以上使用されません。
簡単な@NotNullチェックのために春のバリデータークラスにコードを書きたくないので、 両方の方法を一緒に行う方法はありますか?