简体   繁体   中英

Custom Bean Validator using Spring's Validator interface

I am trying to write a custom bean validator and show validation messages based on locale on the user interface.

To do that, I created a validator like the following:

@Component
public class MyCustomValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return MyClass.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        MyClass myObject = (MyClass)target;

        if (StringUtils.isBlank(myObject.getName())) {          
            errors.rejectValue("name", "{myproject.myclass.validation.name});
        }
    }

}

I have also registered messageSource and validator beans:

@Configuration
@ComponentScan(basePackages = "com.mycompany.myproject")
public class MyProjectWebMvcConfig {

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

        messageSource.addBasenames("locale/myproject");

        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public LocalValidatorFactoryBean validator() {
        LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(this.messageSource());
        return validator;
    }
}

In my controller I used initBinder to register my validators:

@RestController
@RequestMapping("/api/mytype")
public class MyController extends MyBaseController {

    @Autowired
    private MyCustomValidator myCustomValidator;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        super.initBinder(binder);
        binder.addValidators(this.myCustomValidator);
    }

    @PostMapping(value = "/")
    public ResponseEntity<OperationResult<Void>> save(@Valid @RequestBody MyClass myObject, BindingResult bindingResult, HttpServletRequest request) {
        if (!bindingResult.hasErrors()) {
            ...
        }
        ...
    }
}

public class MyBaseController {

    @Autowired
    protected Validator validator;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(validator);
    }
}

Still, validation error messages appear like {myproject.myclass.validation.name} on the user interface. It seems like messages are not read from messageSource even if I have set validation message source of LocalValidatorFactoryBean .

On the other hand, if I use @NotNull(message = {myproject.myclass.validation.name}) annotation instead of using a custom validator, the validation error message appears correctly.

Couldn't figure out what I am missing.

尝试这个

  errors.rejectValue("name", "myproject.myclass.validation.name");

Check encoding of messageSource files. It should be utf-8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM