Quickest way of getting an custom error message in Spring Boot 2.x

There are different ways to get a custom error message page in spring boot. The following solution worked for my web project. Here is the overview of the structure.

Pic Pic

To start with, if there is 404 error, then we want a custom error page showing up instead of the spring boot’s default page.
The first thing we are going handle is to suppress the boot framework’s error page. For this, we will go to the application properties and add the following line


As we are done with that, now let us implement the ErrorController interface. The thing to keep in mind is, we have the LOG entry that says what happened to the code base as we reached here. Here is the sample code.


public class ITContractErrorController implements ErrorController     {
    public static final Logger LOG = LoggerFactory.getLogger(ITContractErrorController.class);
    ErrorAttributes errorAttributes;
    public String handleError() {
        LOG.error("Sever error occured");
        return "/error/error.html";
public String getErrorPath() {
    return "error";

One other way that we can add the custom error message is using the Spring 2.x’s ConfigurableServletWebServerFactory implementation. We add this to the main method of the web module. Here is the sample code for this entry.


public class SkminfycontractorWebmoduleApplication {
    public ConfigurableServletWebServerFactory         containerCustomizer(){
        TomcatServletWebServerFactory factory = new     TomcatServletWebServerFactory();
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,     "/error/error.html"));
        return factory;
    public static void main(String[] args) {
        SpringApplication.run(SkminfycontractorWebmoduleApplication.class, args);

One thing to remember is, when we implement the override with ConfigurableServeletFactory then Override that is ErrorController is gone.

There is a lot of best practice information laid out in spring documentation and other places. But none worked for my setup. I will update this entry if I find a more interesting solution.

Author: Sathish Jayapal

Indie programmer and blogger