content: package com.company.project.common.exception.handler;
  
  import cn.dev33.satoken.exception.NotLoginException;
  import com.company.project.common.exception.BusinessException;
  import com.company.project.common.exception.code.BaseResponseCode;
  import com.company.project.common.utils.DataResult;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.validation.ObjectError;
  import org.springframework.web.bind.MethodArgumentNotValidException;
  import org.springframework.web.bind.annotation.ExceptionHandler;
  import org.springframework.web.bind.annotation.RestControllerAdvice;
  import org.springframework.web.servlet.ModelAndView;
  
  import javax.validation.ConstraintViolation;
  import javax.validation.ConstraintViolationException;
  import java.util.List;
  import java.util.Set;
  
  /**
   * RestExceptionHandler
   *
   * @author wenbin
   * @version V1.0
   * @date 2020年3月18日
   */
  @RestControllerAdvice
  @Slf4j
  public class RestExceptionHandler {
  
      /**
       * 系统繁忙，请稍候再试"
       */
      @ExceptionHandler(Exception.class)
      public DataResult handleException(Exception e) {
          log.error("Exception,exception:{}", e, e);
          return DataResult.getResult(BaseResponseCode.SYSTEM_BUSY);
      }
  
      /**
       * 自定义全局异常处理
       */
      @ExceptionHandler(value = BusinessException.class)
      DataResult businessExceptionHandler(BusinessException e) {
          log.error("Exception,exception:{}", e, e);
          return new DataResult(e.getMessageCode(), e.getDetailMessage());
      }
  
  
      /**
       * 处理validation 框架异常
       */
      @ExceptionHandler(MethodArgumentNotValidException.class)
      DataResult methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
          log.error("methodArgumentNotValidExceptionHandler bindingResult.allErrors():{},exception:{}", e.getBindingResult().getAllErrors(), e);
          List<ObjectError> errors = e.getBindingResult().getAllErrors();
          return DataResult.getResult(BaseResponseCode.METHODARGUMENTNOTVALIDEXCEPTION.getCode(), errors.get(0).getDefaultMessage());
      }
  
      /**
       * 校验List<entity>类型， 需要controller添加@Validated注解
       * 处理Validated List<entity> 异常
       */
      @ExceptionHandler
      public DataResult handle(ConstraintViolationException exception) {
          log.error("methodArgumentNotValidExceptionHandler bindingResult.allErrors():{},exception:{}", exception, exception);
          Set<ConstraintViolation<?>> violations = exception.getConstraintViolations();
          StringBuilder builder = new StringBuilder();
          for (ConstraintViolation<?> violation : violations) {
              builder.append(violation.getMessage());
              break;
          }
          return DataResult.getResult(BaseResponseCode.METHODARGUMENTNOTVALIDEXCEPTION.getCode(), builder.toString());
      }
  
      /**
       * 未登录异常
       */
      @ExceptionHandler
      public ModelAndView handle(NotLoginException exception) {
          log.error("NotLoginException: ", exception);
          // 使用ModelAndView重定向到登录页面
          ModelAndView modelAndView = new ModelAndView("redirect:/index/login");
          return modelAndView;
      }
  
  }
  