springboot全局异常处理

1.自定义错误页面

SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会像/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicExceptionController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息
在这里插入图片描述

  • 如果我们需要将所有的异常挑传到同一个页面就需要在resources/templates ,创建error.html页面。这个名称必须交error
    在这里插入图片描述
  • 发送请求

在这里插入图片描述

2.@ExceptionHandler

@ExceptionHandler可以处理特定异常

  • 针对特定异常
  • 控制器:
@Controller
public class UserController {

    @RequestMapping("/show")
    public String showInfo(Model model){
//        System.out.println(1/0);
        model.addAttribute("msg","Thymeleaf Hello ....122211");
        return "index";
    }
    @RequestMapping("show1")
    public String showInfo2(){
        String str= null;
        System.out.println(str.length());
        return "success";
    }

    /**
     * 如果出现空指针异常就会跳到这个对应的页面
     * @return
     */

    @ExceptionHandler(value = {NullPointerException.class})
    public ModelAndView NullPointerException(Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("error",e.toString());
        modelAndView.setViewName("error1");
        return modelAndView;
    }

    @ExceptionHandler(value = {ArithmeticException.class})
    public ModelAndView ArithmeticException(Exception e){
        ModelAndView view = new ModelAndView();
        view.addObject("error",e.toString());
        view.setViewName("error1");
        return view;
    }



    @RequestMapping("show2")
    public String showInfo1(){
        System.out.println(1/0);
        return "success";
    }

在这里插入图片描述
在这里插入图片描述

耦合性太高

3@ControllerAdvice

和第二种写法基本一样,只不过他更解耦,与控制层分离


import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author 51209
 * @create 2020/9/14
 */


@ControllerAdvice
public class Handler {

    /**
     * 如果出现空指针异常就会跳到这个对应的页面
     * @return
     */
    @ExceptionHandler(value = {NullPointerException.class})
    public ModelAndView NullPointerException(Exception e) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("error",e.toString());
        modelAndView.setViewName("error1");
        return modelAndView;
    }

    @ExceptionHandler(value = {ArithmeticException.class})
    public ModelAndView ArithmeticException(Exception e){
        ModelAndView view = new ModelAndView();
        view.addObject("error",e.toString());
        view.setViewName("error1");
        return view;
    }
}

4.SimpleMappingExceptionResolver

通过系统提供的异常映射实现

 /**
     * 异常信息对应 地址映射
     * @return
     */
    @Bean
    public SimpleMappingExceptionResolver  getSimpleMapping(){
        SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
        Properties mapping = new Properties();
        mapping.setProperty("java.lang.NullPointerException","error1");
        mapping.setProperty("java.lang.ArithmeticException" ,"error2");
        resolver.setExceptionMappings(mapping);
        return  resolver;
    }

5.自定定义异常处理

通过实现

@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {
    /**
     * 自定义全局异常
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @return
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest request,HttpServletResponse response,Object handler, Exception ex) {
        System.out.println("全局异常");
        ModelAndView view = new ModelAndView();
        if (ex instanceof NullPointerException){
            view.setViewName("error1");
            view.addObject("error","空指针异常");
        }
        else if (ex instanceof ArithmeticException){
            view.setViewName("error2");
            view.addObject("error","计算异常");
        }
        return view;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值