Commit ac3bd266f208cacccf76c9b365d9191ef3ee02bd

Authored by changpengfei
1 parent 0a6deaea98
Exists in master and in 1 other branch dev

全局异常处理

Showing 2 changed files with 32 additions and 0 deletions

talkonlineweb/src/main/java/com/lyms/talkonlineweb/config/GlobalExceptionHandler.java View file @ ac3bd26
  1 +package com.lyms.talkonlineweb.config;
  2 +
  3 +import com.lyms.talkonlineweb.result.BaseResponse;
  4 +import lombok.extern.log4j.Log4j2;
  5 +import org.springframework.http.HttpRequest;
  6 +import org.springframework.web.bind.annotation.ControllerAdvice;
  7 +import org.springframework.web.bind.annotation.ExceptionHandler;
  8 +import org.springframework.web.bind.annotation.ResponseBody;
  9 +import org.springframework.web.bind.annotation.RestControllerAdvice;
  10 +
  11 +/**
  12 + * 全局异常处理类
  13 + */
  14 +@RestControllerAdvice
  15 +@Log4j2
  16 +public class GlobalExceptionHandler {
  17 +
  18 + @ExceptionHandler(value = Exception.class)
  19 + @ResponseBody
  20 + public BaseResponse exceptionHandler(Exception e){
  21 + BaseResponse baseResponse=new BaseResponse();
  22 + baseResponse.setErrorcode(1);
  23 + baseResponse.setErrormsg("服务器内部错误:"+e.getMessage());
  24 + log.error(e.getMessage());
  25 + e.printStackTrace();
  26 + return baseResponse;
  27 + }
  28 +}
talkonlineweb/src/main/resources/application.yml View file @ ac3bd26
... ... @@ -11,6 +11,10 @@
11 11 password: platform123
12 12 jackson:
13 13 date-format: yyyy-MM-dd HH:mm:ss
  14 + mvc:
  15 + throw-exception-if-no-handler-found: true
  16 + resources:
  17 + add-mappings: false
14 18 # profiles:
15 19 # active:
16 20 logging: