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: