SpringBoot自定义异常处理
全局异常处理
@ControllerAdvice
@RestController
public class GlobalExceptionInterceptor {//Exception异常
@ExceptionHandler(value = https://www.it610.com/article/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 = https://www.it610.com/article/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;
}}
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- SpringBoot调用公共模块的自定义注解失效的解决
- python自定义封装带颜色的logging模块
- 解决SpringBoot引用别的模块无法注入的问题
- 列出所有自定义的function和view
- python-安装sublime遇到异常
- Android|Android BLE蓝牙连接异常处理
- springboot使用redis缓存
- Spring|Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件
- 自定义MyAdapter