【问题标题】:Which is the class of Spring Boot 404 error?Spring Boot 404 错误属于哪一类?
【发布时间】:2020-01-26 08:17:57
【问题描述】:

我的 Spring Boot 应用程序的多个 URL 没有任何映射。 比如这个:

我有一个异常处理器:

    @RestControllerAdvice(annotations = RestController.class)
    public class RestExceptionHandler {

        @ExceptionHandler(NotFoundException.class)
        protected ResponseEntity<String> handleNotFoundException() {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new NotFoundException().getMessage());
        }
    }

我想在我的处理程序中处理该异常。 NotFound 这里介绍的是我的自定义异常。 哪个是 Whitelabel 错误的类/类型,以及如何像处理自定义 NFE 一样处理它?

【问题讨论】:

  • 嗨,你在用百里香吗?只是想知道如果可能的话如何回答。

标签: java spring-boot exception


【解决方案1】:

这对应于您尝试调用根本未映射的 URL 的情况。

Controller Advice 可以在特定控制器本身产生异常的情况下提供帮助,但在这里,您甚至没有控制器,因此控制器建议是无关紧要的。 Error Page 是 Spring MVC 对此类情况的回退。

如果你想自定义这个错误页面,可以阅读this tutorial

您可能也有兴趣阅读我在 this thread 中的回答,其中我描述了 Spring MVC 如何在后台工作以及何时适用控制器建议。

【讨论】:

    【解决方案2】:

    如果您使用带有 Thymeleaf 的 Spring Boot,您可以:

    1. 排除默认页面将此行添加到属性文件spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
    2. 在 resources/template 目录下创建您的自定义 html 错误页面
    3. 创建一个实现ErrorController 的类来管理您收到的错误类型,并最终基于此添加特定的错误消息。

    您可以在this article on Baeldung 中找到更多详细信息,它会逐步执行此过程。

    【讨论】:

      猜你喜欢
      • 2017-11-02
      • 2017-09-06
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 2017-10-06
      • 2018-07-15
      • 2020-04-12
      相关资源
      最近更新 更多