购物网站黑白,国家免费编程平台,深圳龙岗网站维护,有什么好黄页网站前言
springboot3.2以下可以定义ErrorPageRegistrar将404错误转发到一个接口地址#xff0c;但升级到springboot3.2#xff08;spring6.1#xff09;后,该配置不生效#xff0c;抛出了500错误。
以前的错误页面处理如下#xff1a;
ConditionalOnClass(ErrorPageRegist…前言
springboot3.2以下可以定义ErrorPageRegistrar将404错误转发到一个接口地址但升级到springboot3.2spring6.1后,该配置不生效抛出了500错误。
以前的错误页面处理如下
ConditionalOnClass(ErrorPageRegistry.class)
public class ErrorPageAutoConfiguration implements ErrorPageRegistrar {Overridepublic void registerErrorPages(ErrorPageRegistry registry) {log.info(-----------错误页面路径配置------------);registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, /404),new ErrorPage(HttpStatus.FORBIDDEN, /403),new ErrorPage(HttpStatus.BAD_REQUEST, /400),new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, /500));log.info(-----------错误页面路径配置结束------------);}
}解决方式
通过查看日志发现springboot3.2默认寻找静态资源在找不到资源后抛出了NoResourceFoundException通过ExceptionAdivisor抓取做了处理而并没有对NoResourceFoundException进行特殊处理默认进入了500错误。
ExceptionAdivisor中添加一组对此异常的处理即可 ExceptionHandler(value NoResourceFoundException.class)ResponseStatus(HttpStatus.NOT_FOUND)public ResponseEntity to404Exception(NoResourceFoundException e) {AccessLogUtils.log(SpringUtils.getRequest(), _404);return res(HttpStatus.NOT_FOUND.value(), 找不到资源, e);}