【发布时间】:2015-05-07 23:34:13
【问题描述】:
我想使用 SpringBoot 返回一个自定义的 404 错误,但我希望能够为其添加一些服务器端逻辑,而不仅仅是提供静态页面。
1.我关闭了application.properties中的默认白标页面
error.whitelabel.enabled=false
2。我在resources/templates下添加了一个Thymeleaf error.html@
顺便说一句,这是可行的。该页面已提供,但未调用任何控制器。
3.我创建了一个类 Error 作为“控制器”
package com.noxgroup.nitro.pages;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/error")
public class Error {
@ExceptionHandler
public String index() {
System.out.println("Returning Error");
return "index";
}
}
很遗憾,我在控制台的任何地方都没有看到Returning Error。
我正在使用带有 Spring Boot 的嵌入式 Tomcat。我见过各种选项,但似乎都不起作用,包括使用 @ControllerAdvice、删除 RequestMapping 等。对我来说都不起作用。
【问题讨论】:
标签: spring error-handling http-status-code-404 spring-boot