How to Remove Whitelabel Error Page

Remove Whitelabel Error Page

The “Whitelabel Error Page” is specific to Spring Boot applications. While Spring Boot uses Java, the error page itself is a feature of the framework and not inherent to the Java language. Here’s how you can remove the Whitelabel Error Page in your Spring Boot application using a custom error page.

Step 1: Create a Custom Error Page

We will create a custom error page to remove Whitelabel Error Page.

  • Simplest Way: Create an HTML file named error.html and place it in your src/main/resources/templates directory (if you’re using Thymeleaf or a similar templating engine). Spring Boot will automatically use this file to display errors.
  • Customizing Specific Errors: Create HTML files for specific error codes (e.g., 404.html, 500. html) and place them in the src/main/resources/templates/error.html. Spring Boot will map the error codes to the corresponding pages.

Example (error.html):

<!DOCTYPE html> 
<html> 
<head> 
    <title>Error</title> 
</head> 
<body> 
    <h1>Custom Error Page</h1> 
    <p>Something went wrong. Please try again later.</p> 
</body> 
</html> 

Step 2: Changing Default Settings

Spring Boot usually uses files called application.properties or application.yml to set up how your app works. You can add or adjust things in these files to change those settings. By default, Spring Boot shows a plain error page when something goes wrong. Let’s change that!

If you’re using Maven, copy and paste these lines into your application.properties file:

# Tell Spring Boot where to find our custom templates (error pages)
spring.mvc.view.prefix=/templates/

# What kind of files are our templates (we're using HTML)
spring.mvc.view.suffix=.html

# Turn off the plain error page
server.error.whitelabel.enabled=false

# Tell Spring Boot where our custom error page is
server.error.path=/error

Add the following line to your application.properties file to remove Whitelabel Error Page Not Recommended.

server.error.whitelabel.enabled=false

Caution: Disabling the Whitelabel Page will prevent any error information from being displayed to the user, which is not ideal for debugging or user experience.

Step 3: Creating Error Controller class

Let’s make the Controller class, which is like the manager of our project.

Go to the main part of your project, where all the code is.

  • Make a new folder inside it called “controller“. Think of this folder as a special place for our manager.
  • Inside the “controller” folder, make a new file called “ControllerClass.java“. This file is where we’ll write the instructions for our manager. (You can choose a different name if you want, but we’ll use “ControllerClass.java” as an example.)

Example ( ControllerClass.java )


package com.example.demo.controller; 
  
import org.springframework.boot.web.servlet.error.ErrorController; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.GetMapping; 
  
@Controller
public class ControllerClass implements ErrorController { 
  
    @GetMapping("/error") 
    public String handleError() { 
        return "error_page"; 
    } 
     
} 

Example 2 Advanced Error Handling ( ErrorController.java )

@Controller
public class MyErrorController implements ErrorController {

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request) {
        // Get the error status code
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        
        // Customize error handling based on the status code
        if (statusCode == 404) {
            return "error/404"; // Return a specific view for 404 errors
        } else {
            return "error"; // Return a generic error page
        }
    }
}

Why We Made the Controller Class

The ControllerClass we created acts like a traffic director for error messages in our app. It uses a special tool called ErrorController to handle those errors.

Inside this class, there’s a function called handleError. It has a tag called @GetMapping with the address “/error”. This means that when an error happens in our app, it goes to this address, and the handleError function takes over. It then shows a page called “error_page” to the user, explaining that something went wrong.

Step 4: Run Your App:

  1. Right-click on the name of your project.
  2. Choose “Run as” from the menu.
  3. Select “Spring Boot App.”

Step 5: What You’ll See with Your Custom Error Page:

Now, instead of seeing the plain Whitelabel Error Page, you’ll see your own custom error page when something goes wrong in your app. This means your changes worked!

Remove Whitelabel Error Page Using Existing ErrorController

For example, your existing ErrorController is

@RestController
public class IndexController {

    @RequestMapping(value = "/error")
    public String error() {
        return "Error handling";
    }

}

So, try to change your code to the following:

@RestController
public class IndexController implements ErrorController{

    private static final String PATH = "/error";

    @RequestMapping(value = PATH)
    public String error() {
        return "Error handling";
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }
}

If you are interested in making an Android chat app using Android Studio. Please check out How To Create Chat App in Android Studio