Menu

Custom error page in spring boot web application

Spring provide an out-of-the-box "Whitelabel Error Page". When any error occurs in our Spring application this default error page will appear.

Which look like as below:

Spring default error page

How to disable the OOTB or whitelabel error page in Spring?

We can disable the default whitelabel error page, which is OOTB error page of Spring application by setting a property 'server.error.whitelabel.enabled' to false in application.properties property file.

server.error.whitelabel.enabled=false

How to create custom error page in Spring?

Spring provide a functional interface ErrorController to handle the errors. We need to create a controller class by implementing this interface ErrorController to handle the custom error and show different error message and pages to end users.

Since ErrorController is a functional interface, hence this interface contains only one method 'getErrorPath()'. In Spring 2.3.0 this method has been deprecated and since 2.3.0 we may use property 'server.error.path' to specify the error path. 

This(server.error.path) property we can set in our application.properties file.

server.error.path=/error

Now create a CustomErrorHandlerController class which will implement ErrorController interface. In this class we will create a @RequestMapping for path /error and render the request to our custom error page. Below is the sample code, here we have created customErrorPage() method which is mapped with /error path.

CustomErrorHandlerController.java

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CustomErrorHandlerController implements ErrorController{

	@Override
	public String getErrorPath() {
		return null;
	}
	
	@RequestMapping("/error")
    public String customErrorPage() {
        return "views/error";
    }
}

You may wondering why we have use the default method in this class which is returning null. This is because we have to provide the definition of all the method of an interface otherwise it will generate compile time error.

Now create a error.html page under views, which will appear when any error occur in the Spring application. 

error.html

<html>
  <title>Spring custom error page</title> 
 <body>
  <h1>Ooops!!</h1>
  <div>
  <h3>Something unexpected happened</h3>
  <p>If you are seeing this message frequently then, please report us. </p>

  <br />
  </div>
 </body>
</html>

We have done with changes now run the application and validate the error page functionality. Next time when system will generate any error then you will see this custom error page instead of whitelabel error page. You may also render to different error pages based on errors and their definition 

e.g. error code 404 render to 404-error.html page and 

error 400 render to 400-error.html page.

These all we can handle in customErrorPage method. We need to create an object of HttpServletRequest and check the status using if-else condition and return the respective error page. 


Hope this helps you!

References:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-customize-the-whitelabel-error-page

https://docs.spring.io/spring-boot/docs/current/api/index.html?org/springframework/boot/web/servlet/error/ErrorController.html 

No comments:

Post a Comment