1.  创建异常处理类:com.feiyan.controller包下创建ProjectExceptionAdvice类

  @ControllerAdvice / @RestControllerAdvice  注:根据项目是正常风格还是Rest风格选择不同的注解

  public class ProjectExceptionAdvice

  {

    @ExceptionHandler(Exception.class)  注:定义需要处理的异常的类型

    public Result doException(Exception ex)

    {

      // do thing.......

      return new Result(Code.ERR, null);

    }

  }

项目异常处理方案

1.  业务异常

  ¤  发送对应消息传递给用户,提醒规范操作

2.  系统异常

  ¤  发送固定消息传递给用户,安抚用户

  ¤  发送特定消息给运维人员,提醒维护

  ¤  记录日志

3.  其它异常

  ¤  发送固定消息传递给用户,安抚用户

  ¤  发送特定消息给编程,提醒维护

  ¤  记录日志

com.feiyan包下创建exception目录,目录里创建系统异常类SystemException

  public class SystemException extends RuntimeException

  {

    private Integer code;

    public Integer getCode(){ return code; }

    public void setCode(Integer code){ this.code = code; }

    public SystemException(Integer code, String message)

    {

      super(message);

      this.code = code;

    }

    public SystemException(Integer code, Throwable cause,String message)

    {

      super(message, cause);

      this.code = code;

    }

  }

com.feiyan.exception包下创建系统异常类BusinessException

  public class SystemException extends RuntimeException

  {

    private Integer code;

    public Integer getCode(){ return code; }

    public void setCode(Integer code){ this.code = code; }

    public SystemException(Integer code, String message)

    {

      super(message);

      this.code = code;

    }

    public SystemException(Integer code, Throwable cause,String message)

    {

      super(message, cause);

      this.code = code;

    }

  }

在有可能出现异常的地方抛出添自定义的异常,

在异常处理类添加注解来拦截异常