java
主页 > 软件编程 > java >

SpringBoot自定义错误处理逻辑介绍

2022-10-23 | 佚名 | 点击:

1. 自定义错误页面

将自定义错误页面放在 templates 的 error 文件夹下,SpringBoot 精确匹配错误信息,使用 4xx.html 或者 5xx.html 页面可以打印错误信息

4xx —— 打印 status 及 message 信息

1

2

<h2 th:text="${status}">page not found</h2>

<h3 th:text="${#message}">We Couldn't Find This Page</h3>

5xx—— 打印 message 及 trace 信息

1

2

<h3 th:text="${message}">Something went wrong.</h3>

<p class="nrml-txt" th:text="${trace}">Why not try refreshing you page? Or you can <a href="#" rel="external nofollow" >contact our support</a> if the problem persists.</p>

2. @ControllerAdvice+@ExceptionHandler

自定义全局异常处理类,处理 ArithmeticException 及 NullPointerException 异常

1

2

3

4

5

6

7

8

9

10

11

12

13

14

package com.wanqing.admin.exception;

import lombok.extern.slf4j.Slf4j;

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

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

@Slf4j

@ControllerAdvice // 使用此注释

public class GlobalExceptionHandler {

    @ExceptionHandler({ArithmeticException.class, NullPointerException.class

    }) // 使用此注释,大括号内为可以处理的异常信息

    public String handleArithException(Exception e){

        log.info("异常是:" + e);

        return "login"; // 返回一个视图地址(ModelAndView)

    }

}

原理:

使用 ExceptionHandlerExceptionResolver 异常处理器处理用 @ExceptionHandler 注释的异常

3. 使用@ResponseStatus处理自定义异常

自定义异常类示例代码:

1

2

3

4

5

6

7

8

9

10

11

import org.springframework.http.HttpStatus;

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

@ResponseStatus(value = HttpStatus.FORBIDDEN, reason = "用户数量太多~~") // 异常可以返回状态码信息

public class userToMany extends  RuntimeException{

    // 有参构造器

    public userToMany(String message){

        super(message);

    }

    public userToMany(){

    }

}

原理:

ResponseStatusExceptionResolver 处理器可以处理 @ResponseStatus 注解的异常,得到 @ResponseStatus 注解的信息,调用 response.sendError(statusCode) 方法将错误信息返回,并发送 /error 请求,交由底层处理

sendError —— 表示此次请求立刻结束,发出 /error 请求,SpringBoot 找谁能处理,都不能处理返回默认的错误页

1

2

3

4

5

6

7

8

9

protected ModelAndView applyStatusAndReason(int statusCode, @Nullable String reason, HttpServletResponse response) throws IOException {

    if (!StringUtils.hasLength(reason)) {

        response.sendError(statusCode);

    } else {

        String resolvedReason = this.messageSource != null ? this.messageSource.getMessage(reason, (Object[])null, reason, LocaleContextHolder.getLocale()) : reason;

        response.sendError(statusCode, resolvedReason);

    }

    return new ModelAndView();

}

4. 框架底层异常

使用 DefaultHandlerExceptionResolver 异常处理器能处理 SpringMVC 底层异常,其能处理我异常种类如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) {

    try {

        if (ex instanceof HttpRequestMethodNotSupportedException) {

            return this.handleHttpRequestMethodNotSupported((HttpRequestMethodNotSupportedException)ex, request, response, handler);

        }

        // 此处省略.............

        if (ex instanceof HttpMessageNotWritableException) {

            return this.handleHttpMessageNotWritable((HttpMessageNotWritableException)ex, request, response, handler);

        }

        if (ex instanceof MethodArgumentNotValidException) {

            return this.handleMethodArgumentNotValidException((MethodArgumentNotValidException)ex, request, response, handler);

        }

    } catch (Exception var6) {

        if (this.logger.isWarnEnabled()) {

            this.logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", var6);

        }

    }

    return null;

}

5. 自定义异常解析器

自定义异常解析器需要满足以下:

自定义异常解析器示例代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@Order(value = Ordered.HIGHEST_PRECEDENCE) // 优先级数字越小,优先级越高

@Component // 注册到容器中

public class CustomerHandlerResolver implements HandlerExceptionResolver {

    @Override

    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

        try {

            httpServletResponse.sendError(511, "我不喜欢的错误");

        } catch (IOException e1){

            e1.printStackTrace();

        }

        return new ModelAndView();

    }

}

自定义异常处理器被加入(未调整优先级时,默认加到最后):

6. ErrorViewResolver实现自定义处理异常

交由ErrorViewResolver的情况 :

情况一: response.sendError ,error 请求转给 Controller

情况二: 判断异常请求无人处理,tomcat 底层 response.sendError

ErrorViewResolver处理方法:

BasicErrorController 得到要去的地址后由 ErrorViewResolver 解析,解析规则为拼接状态码等信息

—— 即 ErrorViewResolver 是最后的异常处理, 没人处理的异常,被它处理

原文链接:https://blog.csdn.net/liuwanqing233333/article/details/127291488
相关文章
最新更新