♻️ Refactoring code.增加业务校验异常的全局统一处理

This commit is contained in:
lishangbu 2020-12-29 00:08:54 +08:00
parent ab0e0fb8c1
commit 6ad2447333
1 changed files with 18 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.SpringSecurityMessageSource; import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.util.Assert;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
import org.springframework.validation.FieldError; import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
@ -60,6 +61,23 @@ public class GlobalBizExceptionHandler {
return R.failed(e.getLocalizedMessage()); return R.failed(e.getLocalizedMessage());
} }
/**
* 处理业务校验过程中碰到的非法参数异常 该异常基本由{@link org.springframework.util.Assert}抛出
* @see Assert#hasLength(String, String)
* @see Assert#hasText(String, String)
* @see Assert#isTrue(boolean, String)
* @see Assert#isNull(Object, String)
* @see Assert#notNull(Object, String)
* @param exception 参数校验异常
* @return API返回结果对象包装后的错误输出结果
*/
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.OK)
public R handleIllegalArgumentException(IllegalArgumentException exception) {
log.error("非法参数,ex = {}", exception.getMessage(), exception);
return R.failed(exception.getMessage());
}
/** /**
* AccessDeniedException * AccessDeniedException
* @param e the e * @param e the e