简体   繁体   English

Spring Boot中的自定义错误页面

[英]Custom error page in spring boot

I'm trying to create custom error page in my web-application instead of white-label pages. 我正在尝试在Web应用程序中创建自定义错误页面,而不是白标签页面。 As I know all I need to do in Spring Boot is to add an error.html file under resources/static/templates path, so I did it (I'm using Thymeleaf). 据我所知,我在Spring Boot中需要做的就是在resources / static / templates路径下添加一个error.html文件,所以我做到了(我正在使用Thymeleaf)。 But now instead of white-label pages, it shows me tomcat error pages. 但是现在,它显示的是tomcat错误页面,而不是白标签页面。 I have also tried to add: 我也尝试添加:

server.error.whitelabel.enabled=true

to the properties file, but the result is the same - container error pages. 到属性文件,但结果是相同的-容器错误页面。 What is the right way to do it? 正确的做法是什么?

The simplest way is to add these lines to the web.xml configuration file that redirects all the Tomcat 404 errors to the defined page error.html : 最简单的方法是将这些行添加到web.xml配置文件中,该文件会将所有Tomcat 404错误重定向到定义的页面error.html

<error-page>
    <error-code>404</error-code>
    <location>/error.html</location>
</error-page>
  1. Specify a single URL /errors in web.xml that maps to a method that would handle the error whenever an error is generated 在web.xml中指定一个URL / errors,该URL映射到一个在生成错误时将处理该错误的方法
  2. Create a Controller called ErrorController with a mapping /errors 使用映射/错误创建一个名为ErrorController的控制器
  3. Figure out the HTTP error code at runtime and display a message according to the HTTP error code. 在运行时找出HTTP错误代码,并根据HTTP错误代码显示一条消息。 For instance, if a 404 error is generated, then the user should see a message like 'Resource not found' , whereas for a 500 error, the user should see something on the lines of 'Sorry! 例如,如果生成404错误,则用户应看到类似“未找到资源”的消息,而对于500错误,用户应看到“对不起! An Internal Server Error was generated at our end' 在我们的末端生成了内部服务器错误'

try this link 试试这个链接

http://www.baeldung.com/custom-error-page-spring-mvc http://www.baeldung.com/custom-error-page-spring-mvc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM