Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use validation for parameter verification in SpringBoot

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report