Przeglądaj źródła

新增参数绑定异常全局捕获

codingliang 2 lat temu
rodzic
commit
219178b0d4

+ 15 - 0
src/main/java/com/sqx/common/exception/SqxExceptionHandler.java

@@ -5,6 +5,9 @@ import org.apache.shiro.authz.AuthorizationException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.dao.DuplicateKeyException;
+import org.springframework.http.converter.HttpMessageNotReadableException;
+import org.springframework.validation.BindException;
+import org.springframework.validation.FieldError;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 import org.springframework.web.servlet.NoHandlerFoundException;
@@ -47,6 +50,18 @@ public class SqxExceptionHandler {
 		return Result.error("没有权限,请联系管理员授权");
 	}
 
+	@ExceptionHandler(BindException.class)
+	public Result bindException(BindException ex) {
+		FieldError fieldError = ex.getFieldError();
+		assert fieldError != null;
+		return Result.error(fieldError.getDefaultMessage());
+	}
+
+	@ExceptionHandler(HttpMessageNotReadableException.class)
+	public Result HttpMessageNotReadableException() {
+		return Result.error("请求参数缺失");
+	}
+
 	@ExceptionHandler(Exception.class)
 	public Result handleException(Exception e){
 		logger.error(e.getMessage(), e);