!52 全局异常处理validation Exception

Merge pull request !52 from 孟凡胜/master
This commit is contained in:
pig4cloud.com 2020-04-18 17:55:44 +08:00 committed by Gitee
commit 30e7c369e5
2 changed files with 16 additions and 4 deletions

View File

@ -69,7 +69,7 @@ pig
3. 不接受`功能请求`的 [issue](https://gitee.com/log4j/pig/issues),功能请求可能会被直接关闭。
4. mail: <a href="mailto:pig4cloud@qq.com">pig4cloud@qq.com</a> | <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=2270033969&site=qq&menu=yes"> QQ: 2270033969</a>
4. mail: <a href="mailto:pig4cloud@qq.com">pig4cloud@qq.com</a> | <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=3130998334&site=qq&menu=yes"> QQ: 3130998334</a>
#### 公开课

View File

@ -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());
}
}