1. 先回顾一下Java的异常体系
参考文章:Java中的异常和处理详解
下面几段关于异常的介绍就是来自上面这篇文章(感谢文章作者),为了看着方便,这里直接贴过来了。
2. 自定义异常
先定义一个 BaseException:
public class BaseException extends RuntimeException {
@Getter
private final Integer code;
public BaseException(Integer code, String message) {
super(message);
this.code = code;
}
}
自定义异常 UserNonExistException:
public class NonExistUserException extends BaseException {
public NonExistUserException() {
super(10001, "用户不存在");
}
}
3. 全局异常处理
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Throwable.class)
public Response runtimeExceptionHandler(HttpServletRequest request, Throwable throwable) {
int code = 500;
String msg = null;
if (throwable instanceof BaseException) {
BaseException e = (BaseException) throwable;
code = e.getCode();
msg = e.getMessage();
}else {
log.error("", throwable);
if (throwable.getLocalizedMessage() == null || throwable.getLocalizedMessage().length() > 20) {
msg = "网络异常";
} else {
msg = throwable.getLocalizedMessage();
}
}
return ResponseUtils.error(code, msg);
}
}
Response是自定义的相应码接口。
相关Spring注解介绍:
@ExceptionHandler:标记了使用 runtimeExceptionHandler() 方法来处理 Throwable 异常。
@ControllerAdvice:用于统一拦截异常。
也可以使用 @RestControllerAdvice 注解代替 @ControllerAdvice注解,@RestControllerAdvice 是 @ControllerAdvice 和 @ResponseBody 的组合。
在返回异常信息之前还可以将异常信息记录在数据库中,记录系统运行期间产生的异常及原因,方便之后分析,修改上述代码如下:
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
@Autowired
public GlobalExceptionHandler(DomainEventPublisher domainEventPublisher) {
this.domainEventPublisher = domainEventPublisher;
}
@ExceptionHandler(Throwable.class)
@ResponseBody
public Response runtimeExceptionHandler(HttpServletRequest request, Throwable throwable) {
int code = 500;
String msg = null;
if (throwable instanceof BaseException) {
BaseException e = (BaseException) throwable;
code = e.getCode();
msg = e.getMessage();
}else {
log.error("", throwable);
if (throwable.getLocalizedMessage() == null || throwable.getLocalizedMessage().length() > 20) {
msg = "网络异常";
} else {
msg = throwable.getLocalizedMessage();
}
// 记录异常信息
recordException(request, throwable);
}
return ResponseUtils.error(code, msg);
}
//封装关键异常信息,发布一个事件
private void recordException(HttpServletRequest request, Throwable throwable) {
String url = request.getRequestURL().toString();
String platform = request.getHeader("platform");
List<ImmutablePair<String, String>> headerList = new ArrayList<>(10);
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
headerList.add(new ImmutablePair<>(headerName, headerValue));
}
String headers = JSONObject.toJSONString(headerList);
Map<String, String[]> parameterMap = request.getParameterMap();
String parameter = JSONObject.toJSONString(parameterMap);
domainEventPublisher.publishEvent(new ExceptionLogEvent(url, platform, headers, parameter, throwable));
}
}
ExceptionLogEvent是一个自定义的事件。
recordException()方法只管发布一个保存异常信息的事件,实际保存操作由ExceptionLogEvent事件监听者完成,这样可以保证当前方法不受保存异常操作的影响。
4. 使用全局异常
@Service
public class UserService{
@Resource
private UserMapper userMapper;
public User getUserById(Long id){
User user = userMapper.selectUserById(id);
if(null == user){
throw new NonExistUserException();
}
return user;
}
}
在代码执行时预判可能发生的异常,当发生时直接抛出对应的自定义异常;系统异常只管一层层往上抛就可以。然后都被全局异常处理捕获,然后可以在页面上弹出相应提示或是跳转到异常页面或者做其他处理。