1. Add dependencies

Added directlyhibernate-validator

<dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> < version > 6.0.2. Final < / version > < / dependency >Copy the code

addspring-boot-starter-validation

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> < version > 1.4.0. RELEASE < / version > < / dependency >Copy the code

addspring-boot-starter-web

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
Copy the code

2. Configuration file, if you want to setfail_fastProperties,trueIf one of the parameters fails, it will return. By default, all parameters must be checked

import org.hibernate.validator.HibernateValidator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; import org.springframework.validation.beanvalidation.SpringValidatorAdapter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; / / @configuration Public class ValidatorConfig extends WebMvcConfigurerAdapter {@bean public Validator validator() { ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class) .configure() // Set fail_fast to true. FailFast (true) //.addProperty("hibernate.validator.fail_fast", "true").buildValidatorFactory(); return validatorFactory.getValidator(); } / requestParam way check * * * * @ return * / @ Bean public MethodValidationPostProcessor MethodValidationPostProcessor () { MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor(); methodValidationPostProcessor.setValidator(validator()); return methodValidationPostProcessor; } @Override public org.springframework.validation.Validator getValidator() { return new SpringValidatorAdapter(validator()); }}Copy the code

RequestParam methodValidationPostProcessor is in case of work

Inherits the WebMvcConfigurerAdapter and overrides the getValidator() method to make spring’s request Validator use our failFast configurerAdapter. Specific reference org. Springframework. Web. Servlet. Config. The annotation. WebMvcConfigurationSupport# mvcValidator method

3. Unified exception handling

/** * Hibernate-valid entity class form accept parameter validation failed * @param ex * @return */ @ExceptionHandler(BindException. Class) @responseBody public WebResult validationErrorHandler(BindException ex) { List<String> collect = ex.getBindingResult().getAllErrors() .stream() .map(ObjectError::getDefaultMessage) .collect(Collectors.toList()); return new WebResult(Errors.INCORRECT_PARAM_FORMAT.getError(), StringUtils.join(collect, ";" )); } / hibernate - * * * valid entity class form validation fails to accept parameters * @ param ex * @ return * / @ ExceptionHandler (MethodArgumentNotValidException. Class) @ResponseBody public WebResult validationErrorHandler(MethodArgumentNotValidException ex) { List<String> collect = ex.getBindingResult().getAllErrors() .stream() .map(ObjectError::getDefaultMessage) .collect(Collectors.toList()); return new WebResult(Errors.INCORRECT_PARAM_FORMAT.getError(), StringUtils.join(collect, ";" )); } / parameters calibration RequestParam way * * * * @ param ex * @ return * / @ ExceptionHandler @ ResponseBody (ConstraintViolationException. Class) public WebResult validationErrorHandler(ConstraintViolationException ex) { List<String> errorInformation = ex.getConstraintViolations() .stream() .map(ConstraintViolation::getMessage) .collect(Collectors.toList()); return new WebResult(Errors.INCORRECT_PARAM_FORMAT.getError(),StringUtils.join(errorInformation, ";" )); }Copy the code

Use 4.

For @requestParam, annotate “Validated” on the class or method, or “Valid” before the entity in the parameter if it is accepted by the entity class