共计 1088 个字符,预计需要花费 3 分钟才能阅读完成。
全局异样解决
@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;
}
}
全局 404 拦挡
@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;
}
}
正文完
发表至: springboot
2021-10-09