【发布时间】:2022-12-04 20:12:31
【问题描述】:
我有一个像这样的自定义 ErrorController:
@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping("/error42")
public String handleError(HttpServletRequest request) {
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
System.err.println(status);
if (Objects.isNull(status)) return "error";
int statusCode = Integer.parseInt(status.toString());
String view = switch (statusCode) {
case 403 -> "errors/403";
case 404 -> "errors/404";
case 500 -> "errors/500";
default -> "error";
};
return view;
}
}
然后我设置了 server.error.path 属性,如下所示:
server.error.path=/error42
到目前为止,一切都很好。一切正常。所有错误都通过我的 CustomErrorController。
但是当我将错误路径设置为server.error.path=/error时——当然我将请求映射注释更改为@RequestMapping("/error")——这将不再起作用。
Spring Boot 现在完全忽略了我的 CustomErrorController。我知道,我已经将路径设置为 Spring Boot 通常定义为标准的路径,但是没有办法覆盖它吗?
非常感谢您提供消除这种奇怪行为的任何信息。
【问题讨论】:
标签: spring spring-boot error-handling