SpringBoot3-1错误处理机制Error

springboot错误处理机制

浏览器页面返回的页面是一个有错误信息的页面;其他的客户端返回的是json 数据
可以参照ErrorMvcAutoConfiguraation 配置类

原理:ErrorMvcAutoConfiguraation
添加了以下组件:

  1. DefaultErrorAttributes
  2. BasicErrorController
  3. ErrorPageCustomizer
  4. DefaultErrorViewResolver

详细分析

一旦页面出现4xx或者5xx 的错误;ErrorPageCustomizer 生效,定制错误响应规则(定制错误请求的url);请求/error 会来到BasicErroeController,处理请求

1. ErrorPageCustomizer

注册错误页面:
public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
            ErrorPage errorPage = new ErrorPage(this.dispatcherServletPath.getRelativePath(this.properties.getError().getPath()));
            errorPageRegistry.addErrorPages(new ErrorPage[]{errorPage});
        }
getPath()  获取页面的请求错误页面的请求路径
@Value("${error.path:/error}")
    private String path = "/error";
public String getPath() {
    return this.path;
    }    

产生一个错误请求的url
2. BasicErrorController

public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) {
        return new BasicErrorController(errorAttributes, this.serverProperties.getError(), this.errorViewResolvers);
    }
表明: 处理/error 请求
 @Controller
@RequestMapping({"${server.error
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值