springboot设置全局异常处理(springBoot异常处理捕获返回错误页面)
springboot设置全局异常处理(springBoot异常处理捕获返回错误页面)在templates下新建error.html。如果项目中不存在具体状态码的页面或没有使用x成功匹配的页面时,显示error.html作为错误显示页面。当出现40开头状态码的错误时,显示页面可以命名为40x.html此时如果项目再次出现500错误时候,就会跳转到我们写好的500.html页面给用户做出提示当出现5开头状态码的错误时,显示页面可以命名为5xx.html当出现50开头状态码的错误时,显示页面可以命名为50x.html
Spring Boot项目错误页配置默认情况,Spring Boot项目错误页面如下。当项目实际上线,如果给用户显示这个页面就不是很友好。当系统出现异常时应该给用户显示更加友好的错误页面。
服务器状态500
1.设置具体的状态码页面在resources/templates/下新建error文件夹,在error中新建:状态码.html的页面。例如当出现500时显示的页面为500.html
文件存放位置
此时如果项目再次出现500错误时候,就会跳转到我们写好的500.html页面给用户做出提示
2.使用x进行模糊匹配当出现5开头状态码的错误时,显示页面可以命名为5xx.html
当出现50开头状态码的错误时,显示页面可以命名为50x.html
当出现40开头状态码的错误时,显示页面可以命名为40x.html
3统一错误显示页面在templates下新建error.html。如果项目中不存在具体状态码的页面或没有使用x成功匹配的页面时,显示error.html作为错误显示页面。
SpringMVC异常简介系统中异常包括两类:预期异常(检查型异常)和运行时异常 RuntimeException,前者通过捕获异常从而获取异常信息, 后者主要通过规范代码开发、测试等手段减少运行时异常情况的发生。
系统的 dao、service、controller 出现都通过 throws Exception 向上抛出,最后由 springmvc 前端控制器交由异常处理器进行异常处理,如下图
缺点:只能处理当前Controller中的异常。
@Controller
public class ControllerDemo1 {
@RequestMapping("test1")
public String test1(){
int i = 1/0;
return "success";
}
@RequestMapping("test2")
public String test2(){
String s =null;
System.out.println(s.length());
return "success";
}
@ExceptionHandler(value ={ArithmeticException.class NullPointerException.class} )
public ModelAndView handelException(){
ModelAndView mv =new ModelAndView();
mv.setViewName("error1");//跳转到error1.html页面
return mv;
}
}
2使用:@ControllerAdvice @ExceptionHandler
此处优先级低于局部异常处理器
package com.msb.exceptionhandler;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
/**
* @Author: 迪迦不敌镓
*/
@ControllerAdvice
public class GloableExceptionHandler1 {
@ExceptionHandler(value ={ArithmeticException.class NullPointerException.class} )
public ModelAndView handelException(){
ModelAndView mv =new ModelAndView();
mv.setViewName("error1");
return mv;
}
}
3使用:SimpleMappingExceptionResolver
配置类配置,往spring容器中注入一个 SimpleMappingExceptionResolver,指定跳转页面
/**
* 全局异常
*/
@Configuration
public class GloableException2 {
@Bean
public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver(){
SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
Properties prop = new Properties();
prop.put("java.lang.NullPointerException" "error1");
prop.put("java.lang.ArithmeticException" "error2");
resolver.setExceptionMappings(prop);
return resolver;
}
}
4自定义的HandlerExceptionResolver
/**
* 全局异常
* HandlerExceptionResolve
*/
@Configuration
public class GloableException3 implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest HttpServletResponse httpServletResponse Object o Exception e) {
ModelAndView mv = new ModelAndView();
if(e instanceof NullPointerException){//判断异常类型
mv.setViewName("error1");//跳转页面
}
if(e instanceof ArithmeticException){//判断异常类型
mv.setViewName("error2");//跳转页面
}
mv.addObject("msg" e);
return mv;
}}