Commit 82f268f45e519b477149206ba70ddb5c890e33ad
1 parent
e808a67d
1.统一异常处理
Showing
1 changed file
with
91 additions
and
0 deletions
src/main/java/com/bsth/exception/GlobalExceptionHandler.java
0 → 100644
| 1 | +package com.bsth.exception; | |
| 2 | + | |
| 3 | +import com.bsth.common.ResponseCode; | |
| 4 | +import org.slf4j.Logger; | |
| 5 | +import org.slf4j.LoggerFactory; | |
| 6 | +import org.springframework.security.access.AccessDeniedException; | |
| 7 | +import org.springframework.security.authentication.AccountExpiredException; | |
| 8 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; | |
| 9 | +import org.springframework.validation.BindException; | |
| 10 | +import org.springframework.web.bind.MethodArgumentNotValidException; | |
| 11 | +import org.springframework.web.bind.annotation.ExceptionHandler; | |
| 12 | +import org.springframework.web.bind.annotation.RestControllerAdvice; | |
| 13 | +import org.springframework.web.servlet.NoHandlerFoundException; | |
| 14 | + | |
| 15 | +import java.util.HashMap; | |
| 16 | +import java.util.Map; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * 全局异常处理器 | |
| 20 | + * | |
| 21 | + */ | |
| 22 | +@RestControllerAdvice | |
| 23 | +public class GlobalExceptionHandler | |
| 24 | +{ | |
| 25 | + private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); | |
| 26 | + | |
| 27 | + @ExceptionHandler(NoHandlerFoundException.class) | |
| 28 | + public Map<String, Object> handlerNoFoundException(Exception e) | |
| 29 | + { | |
| 30 | + log.error(e.getMessage(), e); | |
| 31 | + return error("路径不存在,请检查路径是否正确"); | |
| 32 | + } | |
| 33 | + | |
| 34 | + @ExceptionHandler(AccessDeniedException.class) | |
| 35 | + public Map<String, Object> handleAuthorizationException(AccessDeniedException e) | |
| 36 | + { | |
| 37 | + log.error(e.getMessage()); | |
| 38 | + return error("没有权限,请联系管理员授权"); | |
| 39 | + } | |
| 40 | + | |
| 41 | + @ExceptionHandler(AccountExpiredException.class) | |
| 42 | + public Map<String, Object> handleAccountExpiredException(AccountExpiredException e) | |
| 43 | + { | |
| 44 | + log.error(e.getMessage(), e); | |
| 45 | + return error(e.getMessage()); | |
| 46 | + } | |
| 47 | + | |
| 48 | + @ExceptionHandler(UsernameNotFoundException.class) | |
| 49 | + public Map<String, Object> handleUsernameNotFoundException(UsernameNotFoundException e) | |
| 50 | + { | |
| 51 | + log.error(e.getMessage(), e); | |
| 52 | + return error(e.getMessage()); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @ExceptionHandler(Exception.class) | |
| 56 | + public Map<String, Object> handleException(Exception e) | |
| 57 | + { | |
| 58 | + log.error(e.getMessage(), e); | |
| 59 | + return error(e.getMessage()); | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * 自定义验证异常 | |
| 64 | + */ | |
| 65 | + @ExceptionHandler(BindException.class) | |
| 66 | + public Map<String, Object> validatedBindException(BindException e) | |
| 67 | + { | |
| 68 | + log.error(e.getMessage(), e); | |
| 69 | + String message = e.getAllErrors().get(0).getDefaultMessage(); | |
| 70 | + return error(message); | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * 自定义验证异常 | |
| 75 | + */ | |
| 76 | + @ExceptionHandler(MethodArgumentNotValidException.class) | |
| 77 | + public Object validExceptionHandler(MethodArgumentNotValidException e) | |
| 78 | + { | |
| 79 | + log.error(e.getMessage(), e); | |
| 80 | + String message = e.getBindingResult().getFieldError().getDefaultMessage(); | |
| 81 | + return error(message); | |
| 82 | + } | |
| 83 | + | |
| 84 | + private Map<String, Object> error(String message) { | |
| 85 | + Map<String, Object> result = new HashMap<>(); | |
| 86 | + result.put("status", ResponseCode.ERROR); | |
| 87 | + result.put("msg", ""); | |
| 88 | + | |
| 89 | + return result; | |
| 90 | + } | |
| 91 | +} | ... | ... |