From ac3bd266f208cacccf76c9b365d9191ef3ee02bd Mon Sep 17 00:00:00 2001 From: changpengfei Date: Mon, 6 Sep 2021 08:58:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/GlobalExceptionHandler.java | 28 ++++++++++++++++++++++ talkonlineweb/src/main/resources/application.yml | 4 ++++ 2 files changed, 32 insertions(+) create mode 100644 talkonlineweb/src/main/java/com/lyms/talkonlineweb/config/GlobalExceptionHandler.java diff --git a/talkonlineweb/src/main/java/com/lyms/talkonlineweb/config/GlobalExceptionHandler.java b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/config/GlobalExceptionHandler.java new file mode 100644 index 0000000..659db4d --- /dev/null +++ b/talkonlineweb/src/main/java/com/lyms/talkonlineweb/config/GlobalExceptionHandler.java @@ -0,0 +1,28 @@ +package com.lyms.talkonlineweb.config; + +import com.lyms.talkonlineweb.result.BaseResponse; +import lombok.extern.log4j.Log4j2; +import org.springframework.http.HttpRequest; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +/** + * 全局异常处理类 + */ +@RestControllerAdvice +@Log4j2 +public class GlobalExceptionHandler { + + @ExceptionHandler(value = Exception.class) + @ResponseBody + public BaseResponse exceptionHandler(Exception e){ + BaseResponse baseResponse=new BaseResponse(); + baseResponse.setErrorcode(1); + baseResponse.setErrormsg("服务器内部错误:"+e.getMessage()); + log.error(e.getMessage()); + e.printStackTrace(); + return baseResponse; + } +} diff --git a/talkonlineweb/src/main/resources/application.yml b/talkonlineweb/src/main/resources/application.yml index a267f54..c184bec 100644 --- a/talkonlineweb/src/main/resources/application.yml +++ b/talkonlineweb/src/main/resources/application.yml @@ -11,6 +11,10 @@ spring: password: platform123 jackson: date-format: yyyy-MM-dd HH:mm:ss + mvc: + throw-exception-if-no-handler-found: true + resources: + add-mappings: false # profiles: # active: logging: -- 1.8.3.1