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.

src/
 +- 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"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring-boot-example</groupId>
    <artifactId>spring-boot-custom-error-page-example</artifactId>
    <description>here is example of spring boot custom error page example</description>
    <version>1.0-SNAPSHOT</version>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2 404.html

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

2.3 500.html

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

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.
 */
@RestController
public class ErrorPageController {

    @RequestMapping("/demo")
    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.
 */

@SpringBootApplication
@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:

http://localhost:8080/unknow.html

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

http://localhost:8080/demo

/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?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

2 comments. Leave new

Gouri Shankar
February 9, 2018 6:23 am

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 *