How To Disable Or Customize Spring Boot Whitelabel Error Page

In the previous article, we have learned how to make your spring boot application files structure correct to avoid the Whitelabel error page ( Spring Boot Resolve Whitelabel Error Page Example ). But when your spring boot application has errors in source code, it still shows the Whitelabel error page, and you can not see the real error message on the page. This article will tell you how to customize the Whitelabel error page and display related error data to the client users.

1. Disable Whitelabel Error Page.

If you do not like the Whitelabel error page, you can disable it with the following method.

  1. Add below content in spring boot project src / main / resources / application.properties file.
    server.error.whitelabel.enabled=false
  2. You can also add the below content in the above application.properties file to exclude ErrorMvcAutoConfiguration class.
    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
  3. Now when you run the spring boot application again, you will see the below familiar tomcat HTTP status 500 error page instead of the Whitelabel error page. But the below error page is not user-friendly, so we will tell you how to customize the error page.
    HTTP Status 500 — Internal Server Error

2. Customize Spring Boot Application Error Page.

You have two ways to customize the error page in spring boot.

2.1 Create error.html page in src / main / resources / templates folder like below.

  1. Then when your spring boot app runs into an error, the error.html page will be displayed. Below is the error.html file content.
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Web Site Error Page</title>
        </head>
        <body>
            This is web site error page.
        </body>
    </html>

2.2 Create a more detailed custom error page.

  1. The above error Html page is too general, if you want to display more detailed error information on the error page, please follow the below steps.
  2. Create a class that extends org.springframework.boot.web.servlet.error.ErrorController
  3. Override the getErrorPath() method to return the error page url mapping path.
  4. Create a method that will process the /error page url mapping path. You can return either an error page template file’s name or an error data content string using the @ResponseBody annotation.
  5. In the below example, the CustomErrorController class extends ErrorController class. It overrides the method getErrorPath, and adds a method processError.
  6. It adds the @ResponseBody annotation on the processError method when you want to return the error content directly from the processError method. If you want to return a specified template error page, just remove the @ResponseBody annotation from the method.
  7. There is also an error-500.html file added in the src/main/resources/templates folder. This error template file will be used in the custom error controller.
    create-custom-error-controller-class-to-display-detail-error-information
  8. CustomErrorController.java
    package com.dev2qa.example.controller;
    
    import java.util.Map;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.http.HttpServletRequest;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.servlet.error.ErrorAttributes;
    import org.springframework.boot.web.servlet.error.ErrorController;
    import org.springframework.stereotype.Controller;
    import org.springframework.util.Assert;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.context.request.WebRequest;
    
    @Controller
    public class CustomErrorController implements ErrorController {
    
        // ErrorAttributes object is used to save all error attributes value.
        private final ErrorAttributes errorAttributes;
    
        @Autowired
        public CustomErrorController(ErrorAttributes errorAttributes) {
            Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
            this.errorAttributes = errorAttributes;
        }
    
        /* Return the error page path. */
        @Override
        public String getErrorPath() {
            return "/error";
        }
    
        // Handle the /error path invoke.
        @RequestMapping("/error")
       /* @ResponseBody annotation will return the error page content instead of the template error page name. */
        @ResponseBody
        public String processError(HttpServletRequest request, WebRequest webRequest) {
    
            // Get error status code.
            Integer statusCode = (Integer)request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
    
            if(statusCode.intValue() == 500)
            {
                // If you want to return template error page, then remove the @ResponseBody annotation of this method.
                return "error-500.html";
            }else
            {
                // Get error message.
                String message = (String)request.getAttribute(RequestDispatcher.ERROR_MESSAGE);
    
                // Get exception object.
                Exception exception = (Exception)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
    
                // Get error stack trace map object. 
                Map<String, Object> body = errorAttributes.getErrorAttributes(webRequest, true);
                // Extract stack trace string.
                String trace = (String) body.get("trace");
    
                StringBuffer retBuf = new StringBuffer();
                retBuf.append("<pre>");
    
                if(statusCode != null)
                {
                    retBuf.append("Status Code : ");
                    retBuf.append(statusCode);
                }
    
                if(message != null && message.trim().length() > 0)
                {
                    retBuf.append("\n\rError Message : ");
                    retBuf.append(message);
                }
    
                if(trace != null){
                    retBuf.append("\n\rStack Trace : ");
                    retBuf.append(trace);
                }
    
                retBuf.append("</pre>");
    
                return retBuf.toString();
            }
    
        }
    }
  9. error-500.html
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Web Site Error Page</title>
        </head>
        <body>
            This is internal server error page, error status code is 500.
        </body>
    </html>
  10. Below is the custom error page that removes the annotation @ResponseBody from the processError method. Then when the server 500 error occurred, it will display the error-500.html file content.
    This is internal server error page, error status code is 500.
  11. Below is the error page that uses @ResponseBody annotation on processError method, then it will display the processError method returned string on the web page.
    if(statusCode.intValue() == 500)
    {
        // If you want to return template error page, then remove the @ResponseBody annotation of this method.
        return "error-500.html";
    }else
    {
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x