SpringBoot全局异常统一处理

SpringBoot全局异常统一处理


1.SpringBoot默认错误统一处理机制

​ 在基于SpringBoot的Web应用中,对于Http请求处理过程中发生的各种错误,如常见的400、404和500等错误,SpringBoot默认提供了一种映射到错误页面/error的机制来处理所有的错误,并且该页面也由SpringBoot默认提供,不需要开发者自己编写。该页面会显示请求的错误状态码, 以及一些错误原因和消息,如下图分别为SpringBoot默认提供的404错误和500错误页面:
在这里插入图片描述

在这里插入图片描述

​ 上述/error错误页面路径可以理解为SpringBoot默认为我们写了一个模版错误页面,然后默认还写了一个Controller,该Controller中包含一个/error请求地址映射指向该错误页面。当SpringBoot的错误处理机制捕获到请求异常之后,则会将用户的原请求携带上错误信息,然后转发到这个/error页面,页面再显示错误的相关信息。虽然SpringBoot提供了默认的错误显示页面,但是仅使用该默认错误页面会存在大量的局限性:

​ (1)该页面比较简陋,对于用户而言并不友好;
​ (2)500错误暴露了服务器的详细出错原因,存在严重安全隐患;
​ (3)在前后端分离的项目中,客户端需要的不是页面,而是JSON数据。

2.全局异常统一处理

基于上述SpringBoot默认错误处理机制存在的局限性和问题,SpringBoot中提供了@ControllerAdvice@ExceptionHandler两个注解来实现专门对服务器500异常进行自定义处理。使用示例如下:

@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Map globalException(HttpServletRequest request, Exception e) {
        Map<String,Object> map = new HashMap<>();
        map.put("code",500);
        map.put("message",e.getMessage());
        return map;
    }

    @ExceptionHandler(MyException.class)
    @ResponseBody
    public Map myException(HttpServletRequest request, Exception e) {
        Map<String,Object> map = new HashMap<>();
        map.put("code",500);
        map.put("message",e.getMessage());
        return map;
    }
}

@ControllerAdvice注解表示我们定义的是一个控制器增强类,当其他任何控制器发生异常且异常类型符合@ExceptionHandler注解中指定的异常类时,原请求将会被拦截到这个我们自定义的控制器方法中。

在该方法中,我们可以拿到异常信息,于是便可以自定义该如何处理异常,是返回一个我们自定义的模版错误页面,还是返回JSON数据,这将都由我们根据实际应用场景而自己决定。并且我们还可以自定义异常类处理特殊情况。

另外,@ExceptionHandler注解只有一个value参数,为指定的异常类;@ControllerAdvice注解查看源码参数发现我们还可以指定需要拦截的控制器所在的包路径。

3.自定义SpringBoot错误统一处理

上述通过注解实现的控制器增强类虽然可以处理所有异常对应的500错误,但是对于404等错误,却没法捕获和处理。

实际上,在上文提到的SpringBoot默认错误处理机制中,完成任务处理的控制器实际上是SpringBoot在自动配置类中注入的BasicErrorController对象,该类继承AbstractErrorController,而AbstractErrorController又实现了ErrorController接口。

所以其实如果我们自定义一个BasicErrorController控制器,则Spring容器将不会再使用默认提供的BasicErrorController控制器,转而使用我们自定义的错误处理控制器。

3.1 继承AbstractErrorController类

自定义我们自己的BasicErrorController控制器,可以像默认的BasicErrorController一样直接继承AbstractErrorController,甚至可以直接照搬BasicErrorController的代码,根据自己需求做修改即可。如下示例为我自定义的error处理方法,能够获取了一些错误的基本信息,对常规的错误处理和日志记录已经足够:

@Slf4j
@RestController
@Slf4j
@RestController
public class HttpErrorController extends AbstractErrorController {

    private final static String ERROR_PATH = "/error";

    public HttpErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

    @RequestMapping(ERROR_PATH)
    public Map error(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> attributes = getErrorAttributes(request, true);
        //获取日志需要的请求url和详细堆栈错误信息
        String path = attributes.get("path").toString();
        String trace = attributes.get("trace").toString();
        log.error("path:{} trace:{}",path, trace);
        //获取错误时间、状态码和错误描述信息,返回给用户
        Date timestamp = (Date) attributes.get("timestamp");
        Integer status = (Integer) attributes.get("status");
        String error = attributes.get("error").toString();
        Map<String, Object> map = new HashMap<>();
        map.put("code",status);
        map.put("message",error);
        map.put("timestamp",timestamp);
        return map;
    }
}

3.2 实现ErrorController接口

我们也可以直接实现ErrorController类来对默认BasicErrorController控制器进行替换。但是由于ErrorController接口只有一个过时了的方法,没有AbstractErrorController类提供的一些获取错误信息的方法,所以这种方式只能捕获到所有错误,但是不能获取错误的详细信息。

实现ErrorController接口需要实现getErrorPath()方法,返回的路径表示服务器将会重定向到该路径对应的控制器类,本例中为error方法。

@RestController
public class HttpErrorController implements ErrorController {

    private final static String ERROR_PATH = "/error";

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

    @RequestMapping(ERROR_PATH)
    public Map error(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap<>();
        map.put("code","4xx");
        map.put("message","请求错误~");
        return map;
    }
}

因此对于这种方式,一般推荐和上文第一种@ControllerAdvice+@ExceptionHandler注解的方式结合起来使用:

这样@ControllerAdvice声明的增强控制器专门负责对服务器内部异常的500错误进行处理;
而实现了ErrorController接口的这个错误处理控制器专门处理增强控制器不能捕获到的其他404等错误。
这两种方式一起使用并不会冲突,@ControllerAdvice声明的增强控制器会优先捕获异常,不能捕获的部分再由ErrorController接口的实现类处理即可

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCPeuKZgA==,size_20,color_FFFFFF,t_70,g_se,x_16

SpringBoot全局异常准备

首先还是Maven的相关依赖:

  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.17.RELEASE</version>
        <relativePath />
    </parent>
    <dependencies>
        <!-- Spring Boot Web 依赖 核心 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
 
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.41</version>
        </dependency>
    </dependencies>

自定义基础接口类

首先定义一个基础的接口类,自定义的错误描述枚举类需实现该接口。

代码如下:

public interface BaseErrorInfoInterface {
    /** 错误码*/
  String getResultCode();
 
 /** 错误描述*/
  String getResultMsg();
}

自定义枚举类

然后我们这里在自定义一个枚举类,并实现该接口。

代码如下:

public enum CommonEnum implements BaseErrorInfoInterface {
 // 数据操作错误定义
 SUCCESS("200", "成功!"), 
 BODY_NOT_MATCH("400","请求的数据格式不符!"),
 SIGNATURE_NOT_MATCH("401","请求的数字签名不匹配!"),
 NOT_FOUND("404", "未找到该资源!"), 
 INTERNAL_SERVER_ERROR("500", "服务器内部错误!"),
 SERVER_BUSY("503","服务器正忙,请稍后再试!")
 ;
 
 /** 错误码 */
 private String resultCode;
 
 /** 错误描述 */
 private String resultMsg;
 
 CommonEnum(String resultCode, String resultMsg) {
  this.resultCode = resultCode;
  this.resultMsg = resultMsg;
 }
 
 @Override
 public String getResultCode() {
  return resultCode;
 }
 
 @Override
 public String getResultMsg() {
  return resultMsg;
 }
 
}

自定义异常类

然后我们在来自定义一个异常类,用于处理我们发生的业务异常。

代码如下:

public class BizException extends RuntimeException {
 
 private static final long serialVersionUID = 1L;
 
 /**
  * 错误码
  */
 protected String errorCode;
 /**
  * 错误信息
  */
 protected String errorMsg;
 
 public BizException() {
  super();
 }
 
 public BizException(BaseErrorInfoInterface errorInfoInterface) {
  super(errorInfoInterface.getResultCode());
  this.errorCode = errorInfoInterface.getResultCode();
  this.errorMsg = errorInfoInterface.getResultMsg();
 }
 
 public BizException(BaseErrorInfoInterface errorInfoInterface, Throwable cause) {
  super(errorInfoInterface.getResultCode(), cause);
  this.errorCode = errorInfoInterface.getResultCode();
  this.errorMsg = errorInfoInterface.getResultMsg();
 }
 
 public BizException(String errorMsg) {
  super(errorMsg);
  this.errorMsg = errorMsg;
 }
 
 public BizException(String errorCode, String errorMsg) {
  super(errorCode);
  this.errorCode = errorCode;
  this.errorMsg = errorMsg;
 }
 
 public BizException(String errorCode, String errorMsg, Throwable cause) {
  super(errorCode, cause);
  this.errorCode = errorCode;
  this.errorMsg = errorMsg;
 }
 
 
 public String getErrorCode() {
  return errorCode;
 }
 
 public void setErrorCode(String errorCode) {
  this.errorCode = errorCode;
 }
 
 public String getErrorMsg() {
  return errorMsg;
 }
 
 public void setErrorMsg(String errorMsg) {
  this.errorMsg = errorMsg;
 }
 
 public String getMessage() {
  return errorMsg;
 }
 
 @Override
 public Throwable fillInStackTrace() {
  return this;
 }
 
}

自定义数据格式

顺便这里我们定义一下数据的传输格式。

代码如下:

public class ResultBody {
 /**
  * 响应代码
  */
 private String code;
 
 /**
  * 响应消息
  */
 private String message;
 
 /**
  * 响应结果
  */
 private Object result;
 
 public ResultBody() {
 }
 
 public ResultBody(BaseErrorInfoInterface errorInfo) {
  this.code = errorInfo.getResultCode();
  this.message = errorInfo.getResultMsg();
 }
 
 public String getCode() {
  return code;
 }
 
 public void setCode(String code) {
  this.code = code;
 }
 
 public String getMessage() {
  return message;
 }
 
 public void setMessage(String message) {
  this.message = message;
 }
 
 public Object getResult() {
  return result;
 }
 
 public void setResult(Object result) {
  this.result = result;
 }
 
 /**
  * 成功
  * 
  * @return
  */
 public static ResultBody success() {
  return success(null);
 }
 
 /**
  * 成功
  * @param data
  * @return
  */
 public static ResultBody success(Object data) {
  ResultBody rb = new ResultBody();
  rb.setCode(CommonEnum.SUCCESS.getResultCode());
  rb.setMessage(CommonEnum.SUCCESS.getResultMsg());
  rb.setResult(data);
  return rb;
 }
 
 /**
  * 失败
  */
 public static ResultBody error(BaseErrorInfoInterface errorInfo) {
  ResultBody rb = new ResultBody();
  rb.setCode(errorInfo.getResultCode());
  rb.setMessage(errorInfo.getResultMsg());
  rb.setResult(null);
  return rb;
 }
 
 /**
  * 失败
  */
 public static ResultBody error(String code, String message) {
  ResultBody rb = new ResultBody();
  rb.setCode(code);
  rb.setMessage(message);
  rb.setResult(null);
  return rb;
 }
 
 /**
  * 失败
  */
 public static ResultBody error( String message) {
  ResultBody rb = new ResultBody();
  rb.setCode("-1");
  rb.setMessage(message);
  rb.setResult(null);
  return rb;
 }
 
 @Override
 public String toString() {
  return JSONObject.toJSONString(this);
 }
 
}

自定义全局异常处理类

最后我们在来编写一个自定义全局异常处理的类。

代码如下:

@ControllerAdvice
public class GlobalExceptionHandler {
 private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
 
 /**
  * 处理自定义的业务异常
  * @param req
  * @param e
  * @return
  */
    @ExceptionHandler(value = BizException.class)  
    @ResponseBody  
 public  ResultBody bizExceptionHandler(HttpServletRequest req, BizException e){
     logger.error("发生业务异常!原因是:{}",e.getErrorMsg());
     return ResultBody.error(e.getErrorCode(),e.getErrorMsg());
    }
 
 /**
  * 处理空指针的异常
  * @param req
  * @param e
  * @return
  */
 @ExceptionHandler(value =NullPointerException.class)
 @ResponseBody
 public ResultBody exceptionHandler(HttpServletRequest req, NullPointerException e){
  logger.error("发生空指针异常!原因是:",e);
  return ResultBody.error(CommonEnum.BODY_NOT_MATCH);
 }
 
 
    /**
        * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
 @ResponseBody
 public ResultBody exceptionHandler(HttpServletRequest req, Exception e){
     logger.error("未知异常!原因是:",e);
        return ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值