**
* springboot 第一种自定义异常处理方法
* @Author: tibbers
*
//必须注入 Spring 容器中,不然使用的是DefaultErrorAttributes
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
@Override
public Map getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map errorAttributes = super.getErrorAttributes(webRequest, options);
//error 是我的错误html 放在静态资源的目录下
errorAttributes.put("error","出错啦");
return errorAttributes;
}
}
方法二
//必须要注入到 Spring 容器中,不然使用的视图解析器 DefaultErrorViewResolver
@Component
public class MyErrorViewResolver extends DefaultErrorViewResolver {
public MyErrorViewResolver(ApplicationContext applicationContext, WebProperties.Resources resources) {
super(applicationContext, resources);
}
@Override
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map model) {
//视图文件放在资源目录下的123文件中的456.html
return new ModelAndView("123/456",model);
}
}
如果有不完善的地方、或者错误的请指出来
初学SpringBoot 可能讲的不对



