Spring catch calibration parameters exception and unified treatment

Use @Validated, @ Valid, @ NotBlank like, your own Baidu, paper focuses return failure information out of the capture and verification package

reference:

https://mp.weixin.qq.com/s/EaZxYKyC4L_EofWdtyBCpw

https://www.jianshu.com/p/bcc5a3c86480

 

Validation fails to capture the exception message

@ControllerAdvice 
public class WebExceptionHandler {    // Get Request Processing using authentication @Valid path validation failure after requesting entity thrown exception, before looking at the code continues down 
    @ExceptionHandler (BindException.class) 
    @ResponseBody 
    public ResponseVO BindExceptionHandler (E BindException ) { 
        String Message = e.getBindingResult () getAllErrors () Stream () Map (DefaultMessageSourceResolvable :: getDefaultMessage) .collect (Collectors.joining (...)); 
        return new new ResponseVO (Message); 
    } 
    // parameter format processing request after the failure validate thrown on the wrong @RequestParam anomaly is javax.validation.ConstraintViolationException 
    @ExceptionHandler (ConstraintViolationException.class) 
    @ResponseBody

public ResponseVO ConstraintViolationExceptionHandler (ConstraintViolationException E) { String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining()); return new ResponseVO(message); } //处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常。 @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody public ResponseVO MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) { String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining()); return new ResponseVO(message); } }

 

The request parameters given as an example, then, rather than throw BindException ConstraintViolationException and MethodArgumentNotValidException

@RestController 
@RequestMapping ( "Test") 
public class TestController { 

    @GetMapping ( "Refund") 
    public ResponseVO Refund (@Valid RefundRequest Request) throws Exception { 
        return new new ResponseVO (); 
    } 
} 

@Data 
public class RefundRequest the implements the Serializable { 
    @NotBlank (message = "order number can not be empty") 
    Private String orderId; 
    @min (value = 0, the Message = "is the amount of consumption amount can not be negative") 
    Private int orderAmt; 
}

 

If you have multiple verification failed request parameters, then hit the first check fails throwing an exception, not the next check abnormal parameters, configuration is as follows

@Configuration 
public class WebConfig { 
    @Bean 
    public Validator the Validator () { 
        ValidatorFactory validatorFactory = Validation.byProvider (HibernateValidator.class) 
                .configure () 
                // FailFast means where the context validation failure occurs, immediately check the end, no subsequent verification. 
                .failFast (to true) 
                .buildValidatorFactory (); 

        return validatorFactory.getValidator (); 
    } 

    @Bean 
    public MethodValidationPostProcessor methodValidationPostProcessor () { 
        MethodValidationPostProcessor methodValidationPostProcessor new new MethodValidationPostProcessor = (); 
        methodValidationPostProcessor.setValidator (Validator ());
        return methodValidationPostProcessor; 
    } 
}

 

 

 

Use @Validated, @ Valid, @ NotBlank like, your own Baidu, paper focuses return failure information out of the capture and verification package

reference:

https://mp.weixin.qq.com/s/EaZxYKyC4L_EofWdtyBCpw

https://www.jianshu.com/p/bcc5a3c86480

 

Validation fails to capture the exception message

@ControllerAdvice 
public class WebExceptionHandler {    // Get Request Processing using authentication @Valid path validation failure after requesting entity thrown exception, before looking at the code continues down 
    @ExceptionHandler (BindException.class) 
    @ResponseBody 
    public ResponseVO BindExceptionHandler (E BindException ) { 
        String Message = e.getBindingResult () getAllErrors () Stream () Map (DefaultMessageSourceResolvable :: getDefaultMessage) .collect (Collectors.joining (...)); 
        return new new ResponseVO (Message); 
    } 
    // parameter format processing request after the failure validate the thrown exception is an error @RequestParam javax.validation.ConstraintViolationException 
    @ExceptionHandler (ConstraintViolationException.class) 
    @ResponseBody 
    public ResponseVO ConstraintViolationExceptionHandler (ConstraintViolationException E) {

String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining()); return new ResponseVO(message); } //处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常。 @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody public ResponseVO MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) { String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining()); return new ResponseVO(message); } }

 

The request parameters given as an example, then, rather than throw BindException ConstraintViolationException and MethodArgumentNotValidException

@RestController 
@RequestMapping ( "Test") 
public class TestController { 

    @GetMapping ( "Refund") 
    public ResponseVO Refund (@Valid RefundRequest Request) throws Exception { 
        return new new ResponseVO (); 
    } 
} 

@Data 
public class RefundRequest the implements the Serializable { 
    @NotBlank (message = "order number can not be empty") 
    Private String orderId; 
    @min (value = 0, the Message = "is the amount of consumption amount can not be negative") 
    Private int orderAmt; 
}

 

If you have multiple verification failed request parameters, then hit the first check fails throwing an exception, not the next check abnormal parameters, configuration is as follows

@Configuration 
public class WebConfig { 
    @Bean 
    public Validator the Validator () { 
        ValidatorFactory validatorFactory = Validation.byProvider (HibernateValidator.class) 
                .configure () 
                // FailFast means where the context validation failure occurs, immediately check the end, no subsequent verification. 
                .failFast (to true) 
                .buildValidatorFactory (); 

        return validatorFactory.getValidator (); 
    } 

    @Bean 
    public MethodValidationPostProcessor methodValidationPostProcessor () { 
        MethodValidationPostProcessor methodValidationPostProcessor new new MethodValidationPostProcessor = (); 
        methodValidationPostProcessor.setValidator (Validator ());
        return methodValidationPostProcessor;
    }
}

 

 

Guess you like

Origin www.cnblogs.com/panchanggui/p/11758242.html