春季春季装

时间:2020-04-29 12:23:14

标签: java spring

大家下午好 我的问题是平庸的,但是我找不到答案, 我需要为任何无效链接引发自定义错误页面

我使用Java配置,而不是web.хml

我已添加到application.properties

server.error.whitelabel.enabled=false
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

并创建控制器

@Controller
public class MyErrorController implements ErrorController {

    @RequestMapping("/error")
    public String handleError() {
        //do something like logging
        return "error404";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

我使用了本教程https://www.baeldung.com/spring-boot-custom-error-page

这不起作用,您能帮我吗?

1 个答案:

答案 0 :(得分:1)

在application.properties文件中注释此行或删除:

   #spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

来自baeldung.com的文章中的错误信息。应该自动检测到ErrorMvcAutoConfiguration以便自动注册自定义ErrorController

针对那些不相信并降低我的答案的人的更新:
它有效!

enter image description here