@ControllerAdvice
@RestController
public class GlobalExceptionInterceptor {
//Exception 异样
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result exceptionHandler(Exception e){e.printStackTrace();
LogUtil.writeLog("error","log",e.getMessage()+e.getStackTrace());
Result result = Result.errorResult();
if (e.getClass().equals(HttpMediaTypeNotSupportedException.class)){result.setCode(-981);
result.setMsg("申请参数格局谬误");
}else{result.setCode(-998);
result.setMsg("零碎执行产生谬误");
}
return result;
}
// 运行时异样
@ExceptionHandler(value = RuntimeException.class)
@ResponseBody
public Result exceptionHandler(RuntimeException e){e.printStackTrace();
LogUtil.writeLog("error","log",e.getMessage()+e.getStackTrace());
Result result = Result.errorResult();
result.setCode(-997);
result.setMsg("零碎运行产生谬误");
return result;
}
}
@Component
public class ResponseStatusInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {if(response.getStatus()==404){response.sendRedirect(request.getContextPath() + "/frame/error/404");
}
return true;
}
}