java全局异常处理方式

java全局异常处理方式

Scroll Down

java全局异常处理方式

在日常开发中,好的全局异常的处理可以提高用户体验友性。
springmvc中全局处理异常的方式:

1.使用 @ ExceptionHandler 注解
2.实现 HandlerExceptionResolver 接口
3.使用 @controlleradvice 注解

ExceptionHandler 处理异常只能在一个controller里面,不能全局的控制异常
可以实现HandlerexceptionResolver接口,统一处理
也可以使用@ControllerAdvice+ @ ExceptionHandler实现统一处理

@ControllerAdvice+ @ ExceptionHandler

image.png
异常处理:


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @ClassName GlobalExceptionHandle
 * @Description 统一异常返回页面
 * @Author xupeng
 * @Version 1.0
 */
@ControllerAdvice
public class GlobalExceptionHandle {

    public static Log log = LogFactory.getLog(GlobalExceptionHandle.class);
    @ExceptionHandler(value = Exception.class)
    public ModelAndView excetionHandle(Exception exception,HttpServletRequest request, HttpServletResponse response){
        ModelAndView modelAndView = new ModelAndView();
        String url=request.getRequestURL().toString();
        log.error("错误请求:"+url);
        log.error("错误为:"+exception.getMessage());
        modelAndView.addObject("msg", exception.getMessage());
        modelAndView.addObject("url", url);
        modelAndView.addObject("stackTrace", exception.getStackTrace());
        modelAndView.setViewName("500");
        return modelAndView;

    }
}

image.png
错误处理:

mport org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
 * web错误 全局配置
 */
@Controller
public class AppErrorController implements ErrorController {
    private static final String ERROR_PATH = "/error";

    private ErrorAttributes errorAttributes;

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

    @Autowired
    public AppErrorController(ErrorAttributes errorAttributes) {
        this.errorAttributes = errorAttributes;
    }

    /**
     * Web页面错误处理
     */
    @RequestMapping(value = ERROR_PATH, produces = "text/html")
    public String errorPageHandler(HttpServletRequest request, HttpServletResponse response) {
        int status = response.getStatus();
        switch (status) {
            case 403:
                return "403";
            case 404:
                return "404";
            case 500:
                return "500";
        }

        return "index";
    }


    private int getStatus(HttpServletRequest request) {
        Integer status = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (status != null) {
            return status;
        }

        return 500;
    }
}

image.png

原理解析

ControllerAdvice:
在核心控制器DispatcherServlet为我们的每个请求寻找对应的处理器,处理我们的controller的时候,他已经将对应的ControllerAdvice初始化:
image.png
image.png
然后会根据不同的类型进行对应的处理
推荐查看博客:
https://blog.csdn.net/andy_zhang2007/article/details/100041219
image.png