Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use validation for parameter verification in SpringBoot

Shulou Source: shulou.com Published: 2022-05-31 18:59:48 09月27日 Update

This article mainly introduces SpringBoot how to use validation to do parameter verification related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this SpringBoot how to use validation to do parameter verification article will have a harvest, let's take a look.

1. Add dependencies directly add hibernate-validator org.hibernate.validator hibernate-validator 6.0.2.Final add spring-boot-starter-validation org.springframework.boot spring-boot-starter-validation 1.4.0.RELEASE add spring-boot-starter-web org.springframework.boot spring-boot-starter-web 2. Configuration file

If you want to set the fail_fast property, true means that if there is an error in one parameter, it will be returned. By default, all the parameters must be checked, so there must be a configuration file.

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 / * hibernate parameter verification configuration * / @ Configurationpublic class ValidatorConfig extends WebMvcConfigurerAdapter {@ Bean public Validator validator () {ValidatorFactory validatorFactory = Validation.byProvider (HibernateValidator.class) .configure () / / set fail_fast to true, if you want to verify all Set to false or unconfigure .failFast (true) / / .addProperty ("hibernate.validator.fail_fast", "true") .buildValidatorFactory () Return validatorFactory.getValidator (); * requestParam 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 ());}}

In which methodValidationPostProcessor acts on requestParam.

Inherit WebMvcConfigurerAdapter and override the getValidator () method to allow the request of spring to verify that Validator uses our above validator to make the set failFast take effect. For more information, please see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#mvcValidator method.

3. Unified exception handling / * hibernate-valid entity class form acceptance parameter validation failed * @ param ex * @ return * / @ ExceptionHandler (BindException.class) @ ResponseBody public WebResult validationErrorHandler (BindException ex) {List collect = ex.getBindingResult () .getAllErrors () .stream () .map (ObjectError::getDefaultMessage) .validation (Collectors.toList ()) Return new WebResult (Errors.INCORRECT_PARAM_FORMAT.getError (), StringUtils.join (collect, ";")) } / * * hibernate-valid entity class form acceptance parameter validation failed * @ param ex * @ return * / @ ExceptionHandler (MethodArgumentNotValidException.class) @ ResponseBody public WebResult validationErrorHandler (MethodArgumentNotValidException ex) {List collect = ex.getBindingResult () .getAllErrors () .stream () .map (ObjectError::getDefaultMessage) .validation (Collectors.toList ()) Return new WebResult (Errors.INCORRECT_PARAM_FORMAT.getError (), StringUtils.join (collect, ";")) } / * * RequestParam mode parameter check * @ param ex * @ return * / @ ExceptionHandler (ConstraintViolationException.class) @ ResponseBody public WebResult validationErrorHandler (ConstraintViolationException ex) {List errorInformation = ex.getConstraintViolations () .stream () .map (ConstraintViolation::getMessage) .verification (Collectors.toList ()) Return new WebResult (Errors.INCORRECT_PARAM_FORMAT.getError (), StringUtils.join (errorInformation, ";");} 4. Use

If you write parameter verification directly like @ RequestParam, add Validated comments to the class or the corresponding method. If it is accepted by the entity class, add @ Valid before the entity in the parameter.

This is the end of the article on "how to use validation for parameter verification in SpringBoot". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use validation for parameter verification in SpringBoot". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Parameters entities configuration validation methods knowledge content forms files methods articles above values functions attributes easy to operate articles easy to understand more notes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Microsoft Shulou Technology Shulou Tech Info Linux