From 53c90cfc99c30654fe857262100720d2ef719a08 Mon Sep 17 00:00:00 2001 From: xlt Date: Sun, 27 Mar 2022 21:57:44 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E5=9C=A8=E5=85=A8=E5=B1=80=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86=E5=99=A8=E4=B8=AD=E5=BC=95=E5=85=A5?= =?UTF-8?q?DuplicateKeyException=E7=9A=84=E4=B8=BB=E9=94=AE=E5=86=B2?= =?UTF-8?q?=E7=AA=81=E5=BC=82=E5=B8=B8=EF=BC=8C=E6=9D=A5=E5=A4=84=E7=90=86?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=B8=BB=E9=94=AE=E6=88=96UNIQUE=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E7=9A=84=E6=95=B0=E6=8D=AE=E9=87=8D=E5=A4=8D=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/exception/GlobalExceptionHandler.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java index 90b265e27..7a8988cdd 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/exception/GlobalExceptionHandler.java @@ -11,6 +11,7 @@ import com.ruoyi.common.exception.ServiceException; import lombok.extern.slf4j.Slf4j; import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.validation.BindException; +import org.springframework.dao.DuplicateKeyException; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -71,6 +72,16 @@ public class GlobalExceptionHandler { return R.fail(e.getMessage()); } + /** + * 主键或UNIQUE索引,数据重复异常 + */ + @ExceptionHandler(DuplicateKeyException.class) + public R handleDuplicateKeyException(DuplicateKeyException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求地址'{}',数据库中已存在记录'{}'", requestURI, e.getMessage()); + return R.fail(HttpStatus.HTTP_INTERNAL_ERROR, "数据库中已存在该记录,请联系管理员确认"); + } + /** * 业务异常 */