GlobalExceptionHandler.java 1014 B

12345678910111213141516171819202122232425262728293031323334
  1. package com.lqkj.cmlcp.exception;
  2. import com.lqkj.cmlcp.message.Result;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  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. /**
  10. * 全局异常处理
  11. */
  12. @ControllerAdvice
  13. public class GlobalExceptionHandler {
  14. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  15. @ExceptionHandler(Exception.class)
  16. @ResponseBody
  17. public Result<String> resolveException(Exception e) {
  18. logger.error("收到异常", e);
  19. return Result.fail(e.getMessage());
  20. }
  21. @ExceptionHandler(UsernameNotFoundException.class)
  22. @ResponseBody
  23. public Result<String> resolveException(UsernameNotFoundException e) {
  24. logger.error("收到异常", e);
  25. return Result.fail(e.getMessage());
  26. }
  27. }