在后端发生异常或者是请求出错时,前端通常显示如下
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());
}
}