/**
* 参数异常拦截 (model 里面的)
* @author
* @date 2020/11/17 0017
* @param e
* @return GenericResponse
**/
@ExceptionHandler(MethodArgumentNotValidException.class)
public GenericResponse methodArgumentNotValidException(MethodArgumentNotValidException e, HttpServletRequest request) {
List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
StringBuilder builder = new StringBuilder();
for (ObjectError allError : allErrors) {
String message = allError.getDefaultMessage();
builder.append(message);
}
log.error("接口:" + request.getServletPath(), builder.toString());
return GenericResponse.response(false, 1000, builder.toString());
}
/**
* 参数异常拦截 (方法上的)
* @author
* @date 2020/11/17 0017
* @param e
* @return GenericResponse
**/
@ExceptionHandler(ConstraintViolationException.class)
public GenericResponse constraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
StringBuilder builder = new StringBuilder();
Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
for (ConstraintViolation<?> constraintViolation : constraintViolations) {
String message = constraintViolation.getMessage();
builder.append(message);
}
log.error("接口:" + request.getServletPath(), e.toString());
return GenericResponse.response(false, 1000, builder.toString());
}
/**
* @GetMapping("test")
* public GenericResponse test(@Valid AuthRoleMenu authRoleMenu) {
* return null;
* }
* 参数异常拦截(Get请求 ?拼接形式)
* @param e
* @param request
* @return
*/
@ExceptionHandler(BindException.class)
public GenericResponse bindException(BindException e, HttpServletRequest request) {
List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
StringBuilder builder = new StringBuilder();
for (ObjectError allError : allErrors) {
String message = allError.getDefaultMessage();
builder.append(message);
}
log.error("接口:" + request.getServletPath(), builder.toString());
return GenericResponse.response(false, 1000, builder.toString());
}