From a8af8b1ebd97fcca330c2f19d5845a97bb4ac138 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 28 Apr 2025 00:05:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eusers=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E9=94=81=E5=AE=9A=EF=BC=8C=E8=A7=A3=E5=86=B3?= =?UTF-8?q?swagger=E5=8C=85=E7=89=88=E6=9C=AC=E4=BE=9D=E8=B5=96=20?= =?UTF-8?q?=E5=86=B2=E7=AA=81=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=9C=B0=E6=B5=8B?= =?UTF-8?q?=E7=A7=91mo=E5=93=81=E5=91=B3=E9=83=A8=E5=88=86=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GlobalExceptionHandler.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/main/java/com/jdc/jdcproject/exceptionhandler/GlobalExceptionHandler.java diff --git a/src/main/java/com/jdc/jdcproject/exceptionhandler/GlobalExceptionHandler.java b/src/main/java/com/jdc/jdcproject/exceptionhandler/GlobalExceptionHandler.java new file mode 100644 index 0000000..d9d4771 --- /dev/null +++ b/src/main/java/com/jdc/jdcproject/exceptionhandler/GlobalExceptionHandler.java @@ -0,0 +1,39 @@ +package com.jdc.jdcproject.exceptionhandler; + +import com.jdc.jdcproject.utils.Result; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +@ControllerAdvice +@Slf4j +public class GlobalExceptionHandler { + + //指定出现什么异常执行什么方法 + @ExceptionHandler(Exception.class) + @ResponseBody//为了返回数据 + public Result error(Exception e){ + e.printStackTrace(); + return Result.errorResult().message("执行了全局异常处理"); + } + + //指定出现什么异常执行什么方法 + @ExceptionHandler(ArithmeticException.class) + @ResponseBody//为了返回数据 + public Result error(ArithmeticException e){ + e.printStackTrace(); + return Result.errorResult().message("执行了ArithmeticException异常处理"); + } + + //指定出现什么异常执行什么方法 + //自定义异常处理 + @ExceptionHandler(JdcException.class) + @ResponseBody + public Result error(JdcException e){ + log.error(e.getMessage()); + e.printStackTrace(); + return Result.errorResult().code(e.getCode()).message(e.getMsg()); + } + +} \ No newline at end of file