SpringBoot如何自定义错误处理逻辑

发布时间:2022-10-23 17:07:28 作者:iii
来源:亿速云 阅读:194

本篇内容主要讲解“SpringBoot如何自定义错误处理逻辑”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot如何自定义错误处理逻辑”吧!

1. 自定义错误页面

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

4xx —— 打印 status 及 message 信息

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

5xx&mdash;&mdash; 打印 message 及 trace 信息

	<h4 th:text="${message}">Something went wrong.</h4>
	<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 异常

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 注释的异常

SpringBoot如何自定义错误处理逻辑

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

自定义异常类示例代码:

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 &mdash;&mdash; 表示此次请求立刻结束,发出 /error 请求,SpringBoot 找谁能处理,都不能处理返回默认的错误页

    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 底层异常,其能处理我异常种类如下

    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. 自定义异常解析器

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

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

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();
    }
}

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

SpringBoot如何自定义错误处理逻辑

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

交由ErrorViewResolver的情况 :

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

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

ErrorViewResolver处理方法:

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

&mdash;&mdash; 即 ErrorViewResolver 是最后的异常处理, 没人处理的异常,被它处理

到此,相信大家对“SpringBoot如何自定义错误处理逻辑”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. springBoot(7):web开发-错误处理
  2. 如何实现Springboot错误处理机制

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot

上一篇:Bootstrap工具提示插件怎么使用

下一篇:如何使用vue.js制作分页组件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》