SpringBoot异常处理

1.自定义错误界面

SpringBoot默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制。一旦程序中出现了异常 SpringBoot 会向/error 的 url 发送请求。在 springBoot 中提供了一个叫 BasicErrorController 来处理/error 请求,然后跳转到默认显示异常的页面来展示异常信息

SpringBoot异常处理_第1张图片

如 果我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 再src/main/resources/

templates 目录下创建 error.html 页面。注意:名称必须叫 error

1.1controller

/**
 * SpringBoot处理异常方式一:自定义错误页面
 */
@Controller
public class DemoController {
	
	@RequestMapping("/show")
	public String showInfo(){
		String str = null;
		str.length();
		return "index";
	}
	
	@RequestMapping("/show2")
	public String showInfo2(){
		int a = 10/0;
		return "index";
	}
}

1.2错误页面





错误提示页面


	出错了,请与管理员联系。。。
	

 2.整合web访问全局异常处理器

2.1处理思路

 SpringBoot异常处理_第2张图片 

2.2创建全局异常处理器

package com.zhy.exception;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 通过实现HandlerExceptionResolver接口做全局异常处理
 */
//@Component
public class GloableException 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");
    }else if (e instanceof ArithmeticException){
            mv.setViewName("error2");
        }
        mv.addObject("msg",e.toString());
        return mv;
    }
}

2.3错误页面

error1.html




    
    错误提示页面-ArithmeticException


error 1

出错了,请与管理员联系。。。

 error2.html




    
    错误提示页面-NullPointerException


出错了,请与管理员联系。。。


3.整合ajax全局异常处理

3.1创建全局异常处理器

package com.zhy.exception;


import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class JunitException {
    @ResponseBody
    @ExceptionHandler
    public Map junitException(Exception e){
        Map map = new HashMap<>();
        map.put("status",500);
        map.put("msg",e.toString());
        return map;
    }
}

 4.总结

一、非ajax
    public class GloableExceptionHandler 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");
            }else if(e instanceof ArithmeticException){
                mv.setViewName("error2");
            }
            mv.addObject("msg", e.toString());
            return mv;
        }
    }
二、ajax
    @ControllerAdvice
    public class AjaxGlobalExceptionHandler {

        @ResponseBody
        @ExceptionHandler
        public Map errorHandler(Exception e){
            Map map = new HashMap<>();
            map.put("status", 500);
            map.put("msg", e.toString());
            return map;//{status:500, msg:异常信息}
        }
    }

 

你可能感兴趣的:(spring,boot,java,spring)