SpringBoot处理全局统一异常

在后端发生异常或者是请求出错时,前端通常显示如下

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Jun 07 15:38:07 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

对于用户来说非常不友好。

本文主要讲解如何在SpringBoot应用中使用统一异常处理。

实现方式

第一种:使用@ControllerAdvice和@ExceptionHandler注解

第二种: 使用ErrorController类来实现。

第一种:使用@RestControllerAdvice和@ExceptionHandler注解
package com.itheima.reggie.exception;

import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.sql.SQLIntegrityConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = SQLIntegrityConstraintViolationException.class)
    public R<String> sQLIntegrityConstraintViolationException(HttpServletResponse response, SQLIntegrityConstraintViolationException sqlException){
        log.info("SQLIntegrityConstraintViolationException...");
        log.info("错误代码:"  + response.getStatus());
        return R.error(sqlException.getMessage());
    }

    @ExceptionHandler(value = NullPointerException.class)
    public R<String> globalException(HttpServletResponse response, NullPointerException exception){
        log.info("GlobalExceptionHandler...");
        log.info("错误代码:"  + response.getStatus());
        return R.error(exception.getMessage());
    }


}

自定义异常处理

自定义一个异常

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@Accessors(chain = true)
public class ServiceException extends RuntimeException {
    /**
     * 错误码
     */
    protected Integer errorCode;
    /**
     * 错误信息
     */
    protected String errorMsg;
}

显而易见,这个异常继承了RuntimeException,属于运行时异常。
定义过之后,我们就可以和之前处理NullException方式一样处理我们自定义的异常。包括处理其他异常,都是这种方式。直接贴代码。

package com.itheima.reggie.exception;

import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.sql.SQLIntegrityConstraintViolationException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = SQLIntegrityConstraintViolationException.class)
    public R<String> sQLIntegrityConstraintViolationException(HttpServletResponse response, SQLIntegrityConstraintViolationException sqlException){
        log.info("SQLIntegrityConstraintViolationException...");
        log.info("错误代码:"  + response.getStatus());
        return R.error(sqlException.getMessage());
    }

    @ExceptionHandler(value = NullPointerException.class)
    public R<String> globalException(HttpServletResponse response, NullPointerException exception){
        log.info("GlobalExceptionHandler...");
        log.info("错误代码:"  + response.getStatus());
        return R.error(exception.getMessage());
    }

    /**
     * 处理自定义的业务异常
     * @param response
     * @param exception
     * @return
     */
    @ExceptionHandler(value = ServiceException.class)
    public R<String> bizExceptionHandler(HttpServletResponse response, ServiceException exception){
        log.error("发生业务异常!原因是:{}", exception.getErrorMsg());
        return R.error(exception.getErrorMsg());
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值