Spring Boot默认提供了一种映射到错误页面/error的机制来处理所有的错误,可以映射到名为404或500等的错误页面,但此种机制存在局限性。为此,SpringBoot提供了注解的方式自定义统一处理服务器500错误机制。
@ControllerAdvice注解表示我们定义的是一个控制器增强类,当其他任何控制器发生异常且异常类型符合@ExceptionHandler注解中指定的异常类时,原请求将会被拦截到这个我们自定义的控制器方法中。 其annotations参数表示我们要拦截的带有指定注解的类或方法
在@ExceptionHandler所注解的方法中,只有一个value参数,为指定的异常类;常用的三个参数Exception、HttpServletRequset、HttpServletResponse便可解决大部分问题
在向浏览器做出响应时应注意:浏览器的请求方式是普通的请求方式?还是异步请求方式?
这里我们从请求头里获取请求方式:request.getHeader("x-requested-with");
如果是异步请求则返回json格式的字符串,如果是普通请求则重定向至html页面
具体代码实现如下:
(此代码实现拦截所有带有@Controller注解的Exception类型的错误,并记录日志、做出响应)
import com.nowcoder.community.community.util.CommunityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
// ()内容表示:扫描带有Controller注解的Bean
@ControllerAdvice(annotations = Controller.class)
public class ExceptionAdvice {
private static final Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);
@ExceptionHandler({Exception.class})
public void handleException(Exception e, HttpServletRequest request, HttpServletResponse response) throws IOException {
// 记录异常的概括
logger.error("服务器发生异常:" + e.getMessage());
// 记录详细的栈中异常
for (StackTraceElement element : e.getStackTrace()){
logger.error(element.toString());
}
// 给浏览器响应(响应前需要判断请求的方式)
String xRequestedWith = request.getHeader("x-requested-with");
if("XMLHttpRequest".equals(xRequestedWith)){
// 异步请求,向浏览器响应字符串
response.setContentType("application/plain;charset=utf-8");// "plain"表示普通字符串,也可"json"表示json字符串
PrintWriter writer = response.getWriter();
writer.write(CommunityUtil.getJSONString(1,"服务器异常!"));
// CommunityUtil为自定义的工具类,getJSONString为此类中获取JSON字符串的方法
} else {
// 普通请求,向浏览器响应html页面
response.sendRedirect(request.getContextPath() + "/error");
}
}
}
在实际项目中,异常可以分为以下三类:
① 业务异常(BusinessException)
规范的用户行为产生异常
不规范的用户行为操作产生的异常
解决办法:发送对应消息给用户,提醒规范操作
② 系统异常(SystemException)
项目运行过程中可预计且无法避免的异常
解决办法:(1)发送固定消息传递给用户,安抚用户;(2)发送特定消息给运维人员,提醒维护;(3)记录日志
③ 其他异常(Exception)
编程人员未预期到的异常
解决办法:(1)发送固定消息传递给用户,安抚用户;(2)发送特定消息给运维人员,提醒维护(纳入预期范围内);(3)记录日志
解决的具体实现如下:
① 自定义异常:BusinessException.java(业务级)、SystemException.java(系统级)
/**
* 自定义业务级异常
*/
public class BusinessException extends RuntimeException{
private Integer code;// 记录异常状态码
public Integer getCode() { return code; }
public void setCode(Integer code) { this.code = code; }
// 实现五个构造方法
public BusinessException(Integer code) { this.code = code; }
public BusinessException(Integer code, String message) { super(message);this.code = code; }
public BusinessException(Integer code, String message, Throwable cause) { super(message, cause);this.code = code; }
public BusinessException(Integer code, Throwable cause) { super(cause);this.code = code; }
public BusinessException(Integer code, String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace);this.code = code; }
}
/**
* 自定义系统级异常
*/
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) { this.code = code; }
public SystemException(Integer code, String message) { super(message);this.code = code; }
public SystemException(Integer code, String message, Throwable cause) { super(message, cause);this.code = code; }
public SystemException(Integer code, Throwable cause) { super(cause);this.code = code; }
public SystemException(Integer code, String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace);this.code = code; }
}
② 自定义异常编码(可持续补充)
public class Code {
// 自定义的(可自定义补充)
public static final Integer SYSTEM_ERROR = 50001;// 系统级异常码
public static final Integer SYSTEM_TIMEOUT_ERROR = 50002;// 系统超时异常码
public static final Integer SYSTEM_UNKONW_ERROR = 59999;// 系统未知异常码
public static final Integer BUSINESS_ERROR = 60002;// 业务级异常码
}
这里,我们实现一个 表现层 与 前端 传输数据的协议类: Result 类(选用):
public class Result {
private Integer code;// 状态码
private String message;// 要传给用户的提示信息
private Object data;// 数据
// 构造方法
public Result() {}
public Result(Integer code, String message) { this.code = code;this.message = message; }
public Result(Integer code, String message, Object data) { this.code = code;this.message = message;this.data = data; }
// 实现 getter、setter
public Integer getCode() { return code; }
public void setCode(Integer code) { this.code = code; }
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
public Object getData() { return data; }
public void setData(Object data) { this.data = data; }
}
③ 拦截并处理异常(三部分)
/**
* 统一异常处理类
*/
@RestControllerAdvice
public class ExceptAdvice {
/**
* 处理自定义业务级异常(BusinessException)
* @param exception
*/
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException exception) {
// 1. 安抚用户
return new Result(exception.getCode(), exception.getMessage(), null);
}
/**
* 处理自定义系统级异常(SystemException)
* @param exception
*/
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException exception) {
// 1. 记录日志
// ..............
// 2. 发送消息给运维、发送邮件给开发人员
// ..............
// 3. 安抚用户
return new Result(exception.getCode(), exception.getMessage(), null);
}
/**
* 处理其他的所有异常
* @param exception
*/
@ExceptionHandler(Exception.class)
public Result doException(Exception exception) {
// 1. 记录日志
// ..............
// 2. 发送消息给运维、发送邮件给开发人员
// ..............
// 3. 安抚用户
return new Result(Code.SYSTEM_UNKONW_ERROR, "系统繁忙, 请稍后再试!!!", null);
}
}
④ 模拟触发自定义异常
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public boolean selectById(Integer id) {
// 1. 模拟业务级异常(将可能出现的异常进行包装,装换成自定义异常)
if(id == 1) {
throw new BusinessException(Code.BUSINESS_ERROR, "请不要使用你的技术挑战我的耐性!!!");
}
// 2. 模拟系统级异常(将可能出现的异常进行包装,装换成自定义异常)
try {
int i = 1 / 0;
} catch (Exception e) {
throw new SystemException(Code.SYSTEM_ERROR, "服务器访问超时, 请重试!!!", e);
}
bookDao.selectById(id);
return true;
}
}
⑤ 结果