1. Overview

During web application development, Resource playing important roles so many times those resources may be moved to somewhere else or not available at the particular location at that time server will return default error page to the browser, handle those types of pages are too much important and display a proper message to the user. Spring boot provide easy was to custom error pages. Here is complete example of Spring boot custom error pages:

For example, if 404 want to override then inside resources public/error/  create 404.html page, Name of page like STATUS_CODE.html and remaining things will be handled by spring boot.

 +- main/
     +- java/
     |   + <source code>
     +- resources/
         +- public/
             +- error/
             |   +- 404.html
             +- <other public assets>

2. Example

spring boot custom error page example

spring boot custom error page example


2.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <description>here is example of spring boot custom error page example</description>
    <!-- Inherit defaults from Spring Boot -->
    <!-- Package as an executable jar -->

2.2 404.html

        <title>Spring boot custom error page example</title>
        <h1>This is Spring boot custom error page demo page - 404 status code</h1>

2.3 500.html

        <title>Spring boot custom error page example</title>
        <h1>This is Spring boot custom error page demo page - 500 status code</h1>

2.4 ErrorPageController

package com.javadeveloperzone.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 * Created by Lenovo on 19-07-2017.
public class ErrorPageController {
    public String SpringBootHello() {
        int i=5/0;          // it will throws exception  ArithmeticException: / by zero
        return "demo";

2.5 SpringBootConfig

package com.javadeveloperzone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
 * Created by JavaDeveloperZone on 19-07-2017.
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application

2.6 Output:


The application does not contain unknow.html so spring boot application will return 400 error code and return 400.html to the browser.

spring boot custom error page example - 404

spring boot custom error page example – 404


/demo will throws ArithmeticException so spring boot will return 500(Internal Server Error) status code and display 500.html page.


spring boot custom error page example - 404

spring boot custom error page example – 404

3. References

4. Source Code

Spring boot custom error pages.zip (68 KB)



Was this post helpful?

2 comments. Leave new

Very good article. It solved my issue. Thank you.

Welcome Gouri Shankar. JavaDeveloperZone is trying to make developer life easy.

Leave a Reply

Your email address will not be published. Required fields are marked *