贴一下我司的实现
@
Override public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception e) {
BusinessException exception;
ErrorRes errorRes;
if (e instanceof BusinessException) {
exception = (BusinessException) e;
errorRes = new ErrorRes("" + exception.getErrorCode(),
exception.getMessage(), exception.getErrorParameters());
logger.warn(
errMsgLogid
+ String.format(
"warn code:%s, errorMessage:%s, errorParameters:%s",
exception.getErrorCode(),
exception.getMessage(),
exception.getErrorParameters()), e);
} else {
exception = new BusinessException(ErrorCode.SYSTEM_ERROR, e);
errorRes = new ErrorRes("" + exception.getErrorCode(),
exception.getMessage());
// 获取异常名称
String exceptionName = e.getClass().getName();
if ("org.apache.catalina.connector.ClientAbortException"
.equalsIgnoreCase(exceptionName)) {
logger.warn(errMsgLogid + "system warn:", e);
} else if ("javax.ws.rs.ClientErrorException"
.equalsIgnoreCase(exceptionName)) {
logger.warn(errMsgLogid + "system warn:", e);
} else {
logger.error(errMsgLogid + "system error:", e);
}
}
PrintWriter responseWriter = null;
try {
response.setContentType("application/json;");
response.setCharacterEncoding("UTF-8");
response.setStatus(Response.SC_OK);
responseWriter = response.getWriter();
responseWriter.write(JSON.toJSONString(errorRes));
responseWriter.flush();
} catch (IOException e1) {
logger.error(errMsgLogid, e1);
} finally {
if (responseWriter != null) {
responseWriter.close();
}
}
return new ModelAndView();
}