Using both JSR-303 and Traditional Bean Validation?

前端 未结 3 890
陌清茗
陌清茗 2020-12-31 13:59

Is it possible to use both JSR-303 bean validation and traditional validation (a single validator class for the type) in Spring? If so, what configuration is required to se

3条回答
  •  天命终不由人
    2020-12-31 14:30

    I realise this is quite old, but I got this to work with minimal disturbance to my code

    Change binder.setValidator(new DualEntryValidator());

    to

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.addValidators(new DualEntryValidator());
    }
    

    With setValidator() you're replacing the JSR-303 validator with your one. With addValidator(), the JSR-303 validator is called and so is yours.

    You need to make sure that your validator does not overlap with your JSR-303 @NotNull, @Min, @Max, etc. annotations otherwise you'll get duplicate error messages added.

提交回复
热议问题