mirror of https://gitee.com/maxjhandsome/pig
validation Exception :全局异常处理
当后台用对象(非@response)接收参数时,抛出BindException,该异常被bodyValidExceptionHandler方法拦截处理时无法识别改方法的参数(MethodArgumentNotValidException)
This commit is contained in:
parent
38e12601b7
commit
80dfe8efa9
|
@ -52,17 +52,29 @@ public class GlobalExceptionHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* validation Exception
|
||||
* validation Exception (以json形式传参)
|
||||
*
|
||||
* @param exception
|
||||
* @return R
|
||||
*/
|
||||
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
|
||||
@ExceptionHandler({MethodArgumentNotValidException.class})
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public R bodyValidExceptionHandler(MethodArgumentNotValidException exception) {
|
||||
List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
|
||||
log.warn(fieldErrors.get(0).getDefaultMessage());
|
||||
return R.failed(fieldErrors.get(0).getDefaultMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* validation Exception (以form-data形式传参)
|
||||
*
|
||||
* @param exception
|
||||
* @return R
|
||||
*/
|
||||
@ExceptionHandler({BindException.class})
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public R bindExceptionHandler(BindException exception) {
|
||||
List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
|
||||
log.warn(fieldErrors.get(0).getDefaultMessage());
|
||||
return R.failed(fieldErrors.get(0).getDefaultMessage());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue