Commit ac3bd266f208cacccf76c9b365d9191ef3ee02bd
1 parent
0a6deaea98
Exists in
master
and in
1 other branch
全局异常处理
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