框架 Spring Boot 技术入门到整合 7-3 Springboot配置全局的异常捕获 -同时兼容web和ajax

0    课程地址

https://www.imooc.com/video/16725

1    课程demo
1.1  课程demo(前两节的一个整合)

ErrorHandler.java

package com.example.demo.exception;

import com.example.demo.tools.JSONResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ErrorHandler
 *
 * @author 魏豆豆
 * @date 2020/12/6
 */
@Controller
/**
 *@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping 注解
 */

@RequestMapping("/error")
public class ErrorHandler {

    @RequestMapping("/err")
    public String error(){
        int a = 1/0;
        return "templates/error";
    }

    @RequestMapping("/errAjax")
    public String errorAjax(){
        return "thymeleaf/ajaxerror";
    }

    @RequestMapping("/getAjaxerror")
    @ResponseBody
    public JSONResult getAjaxerror(){
        int a = 1/0;
        return JSONResult.ok();

    }
}

IMoocExceptionHandler.java

package com.example.demo.exception;

import com.example.demo.tools.JSONResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * IMoocExceptionHandler
 *
 * @author 魏豆豆
 * @date 2020/12/6
 */
@RestControllerAdvice
public class IMoocExceptionHandler {

    //定义报错常量
    public static final String ERROR_VIEW = "error";


    //@ExceptionHandler(value = Exception.class)
    /*public Object errorHandler(HttpServletRequest request, HttpServletResponse response,Exception e) throws Exception{
        e.printStackTrace();

        ModelAndView mav = new ModelAndView();
        mav.addObject("url",request.getRequestURI());
        mav.addObject("exception",e);
        mav.setViewName(ERROR_VIEW);
        return mav;
    }*/


    /**
     * 统一处理 ajaxx和web异常
     * @param request
     * @param response
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    public Object errorHandler(HttpServletRequest request, HttpServletResponse response,Exception e) throws Exception{
        e.printStackTrace();

        if(isAjax(request)){
            return JSONResult.errorException(e.getMessage());
        }else{
            ModelAndView mav = new ModelAndView();
            mav.addObject("url",request.getRequestURI());
            mav.addObject("exception",e);
            mav.setViewName(ERROR_VIEW);
            return mav;
        }
    }

    /**
     * 判断是否是 ajax请求
     * @param request
     * @return
     */
    public boolean isAjax(HttpServletRequest request){
        return (request.getHeader("X-Requested-With")!=null
                &&"XMLHttpRequest".equals(request.getHeader("X-Requested-With").toString()));
    }

}

测试结果:

 

原文地址:https://www.cnblogs.com/1446358788-qq/p/14123945.html