SpringBoot的异常处理

     如何对项目的异常进行处理,在这里我做个总结,如果项目的异常不进行处理,就把异常信息直接抛给用户,用户体验度就会很差很差,如果对异常进行处理的,还能帮助开发人员对程序的分析和解决问题提供了直观的反馈。这就是为什么要对异常进行处理的原因(个人理解)

    首先,springboot默认提供了对异常的处理 有两个地方可以存放展示给用户看的错误页面,一个是在static下,一个是在templates下,我这里是放在templates下  如下图所示:

                                                         

    springboot会自动捕获404 、500等错误信息,会从static 和templates 查找是否有对应的404.html 或500.html 如果查找不到,springboot有一套错误页面,就会将自带的错误页面响应回浏览器。

    这是SpringBoot最简单的异常页面处理,但是这样的话 ,作为我们开发人员来说,有些异常信息是我们意想不到的或者是这些异常不是404 或 500 ,那么我们应该怎么样才能跳转到指定的错误页面呢??? 如果可以在异常页面中可以直接查看是出现了什么异常,调用了哪个方法,或者是访问了什么路径出现的异常,那么就不用去服务器中查看日志文件了,于是springboot官方提供了一个解决方案,使用thymeleaf 模板在html中可以直接查看,源码与效果如下:

  error.html  

   
  <div>
        <div th:utext="'<!--'" th:remove="tag"></div>
        <div th:utext="'Failet Request URL : ' + ${url}" th:remove="tag"></div>
        <div th:utext="'Exception message : ' + ${exception.message}" th:remove="tag"></div>
        <ul th:remove="tag">
            <li th:each="st : ${exception.stackTrace}" th:remove="tag"><span th:utext="${st}" th:remove="tag"></span></li>
        </ul>
        <div th:utext="'--$gt'" th:remove="tag"></div>
    </div>


   最终达到的目的:

        

上面是展示给用户看的 下面是开发人员看的

    

这样我们就可以不用去查看服务器日志来判断是什么引起的异常。

 那么我们就来看一下是怎么实现的!

    首先我们先明确一下,404、500信息还是交给springboot默认处理方式,就是通过查找404页面或者500页面,其他异常我们自己处理。

    首先我们先自己定义一个异常类,继承运行时异常类

如下:

    源码:

package com.lf.blog;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class NotFundException extends RuntimeException {
    public NotFundException() {
    }

    public NotFundException(String message) {
        super(message);
    }

    public NotFundException(String message, Throwable cause) {
        super(message, cause);
    }
}


    然后我们定义一个异常控制类

                                                    

源码:

package com.lf.blog.Handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice //会拦截所有controller注解的控制器
public class ControllerExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(Exception.class)
    public ModelAndView ExceptionHandler(HttpServletRequest request,Exception e) throws Exception {
        logger.error("request URL : {}, Exception : {}",request.getRequestURI(),e);

        if(AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null){
            throw e;
        }

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


现在我们来试一下是否可行

    

然后我们来访问页面 这是我伪造的找不到页面的demo


出现404 那么这个方式是可行的,这样的话就能对异常有一个全局可控的状态了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章