In this tutorials I am going to show how to create our own custom Spring Boot Error Page instead of showing generic error pages like while label error.

Custom Spring Boot Error Page :

Technologies :

  • Spring_Boot_1.5.10.RELEASE
  • Java8
  • IntelliJ Idea

Project Structure :

Spring Boot Error Page

pom.xml

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>com.onlinetutorialspoint</groupId>
  <artifactId>SpringBoot_Custom_ErrorPage</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringBoot_Custom_ErrorPage</name>
  <description>Demo project for Spring Boot</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

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

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

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Spring Boot Custom Error Page Example :

Creating Error pages like 404 (Page Not Found) and 500 (Internal Server) and Etc..

Note : These pages should be available in classpath (resources).

400.html :

404.html
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>404 Page no found</title>
    <meta name="viewport" content="width=device-width">
    <style>
        #error {
            border-color: darkred;
            background-color: aliceblue;
        }
        h2 {
            color: green;
        }
    </style>

</head>
<body>
<div class="error-page-wrap">
    <div id="error">
        <h2>oops! Requested page not found</h2>
    </div>
</div>
</body>
</html>

500.html

500.html
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>500 Page</title>
    <meta name="viewport" content="width=device-width">
    <style>
        #error {
            border-color: darkred;
            background-color: aliceblue;
        }

        h3 {
            color: darkred;
        }
    </style>

</head>
<body>
<div class="error-page-wrap">
    <div id="error">
        <h3>Something went wrong ! Please try again later or contact our support @ 123456789</h3>
    </div>
</div>
</body>
</html>

Create Rest Controller :

Create your Spring Boot rest controller to access end points.

MainController.java
package com.onlinetutorialspoint.SpringBoot_Custom_ErrorPage;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainController {
    @RequestMapping("/hello")
    public String sayHello() {
        int i = 5 / 0;
        return "hello Greetings !";
    }

    @RequestMapping("/user")
    public String user() {
        return "Hello user Greetings !";
    }
}

Application.java

Application.java
package com.onlinetutorialspoint.SpringBoot_Custom_ErrorPage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

Run It :

Terminal
mvn clean install 
mvn spring-boot:run


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.10.RELEASE)

2018-02-16 21:45:30.060  INFO 11240 --- [           main] c.o.S.Application                        : Starting Application on DESKTOP-RN4SMHT with PID 11240 (E:\work\SpringBoot_Custom
_ErrorPage\target\classes started by Lenovo in E:\work\SpringBoot_Custom_ErrorPage)
2018-02-16 21:45:30.060  INFO 11240 --- [           main] c.o.S.Application                        : No active profile set, falling back to default profiles: default
2018-02-16 21:45:30.169  INFO 11240 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebA
pplicationContext@1738bc53: startup date [Fri Feb 16 21:45:30 IST 2018]; root of context hierarchy
2018-02-16 21:45:32.794  INFO 11240 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-02-16 21:45:32.825  INFO 11240 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-02-16 21:45:32.825  INFO 11240 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.27
2018-02-16 21:45:32.997  INFO 11240 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-02-16 21:45:32.997  INFO 11240 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2844 ms
2018-02-16 21:45:33.294  INFO 11240 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-02-16 21:45:33.310  INFO 11240 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-02-16 21:45:33.310  INFO 11240 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-02-16 21:45:33.310  INFO 11240 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-02-16 21:45:33.310  INFO 11240 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-02-16 21:45:33.825  INFO 11240 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.Annotati
onConfigEmbeddedWebApplicationContext@1738bc53: startup date [Fri Feb 16 21:45:30 IST 2018]; root of context hierarchy
2018-02-16 21:45:33.935  INFO 11240 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/user]}" onto public java.lang.String com.onlinetutorialspoint.SpringBo
ot_Custom_ErrorPage.MainController.user()
2018-02-16 21:45:33.950  INFO 11240 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.onlinetutorialspoint.SpringB
oot_Custom_ErrorPage.MainController.sayHello()
2018-02-16 21:45:33.950  INFO 11240 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util
.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-02-16 21:45:33.950  INFO 11240 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=}" onto public org.springframework.web.serv
let.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-02-16 21:45:34.013  INFO 11240 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web
.servlet.resource.ResourceHttpRequestHandler]
2018-02-16 21:45:34.013  INFO 11240 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet
.resource.ResourceHttpRequestHandler]
2018-02-16 21:45:34.075  INFO 11240 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework
.web.servlet.resource.ResourceHttpRequestHandler]

Access application :

Accessing unknown end point :

http://localhost:8080/unknown

Spring Boot Error Page 404

Internal Server Error handling :

http://localhost:8080/hello

Spring Boot Error Page 500

Accessing valid endpoint :

http://localhost:8080/user

Spring Boot Error Page Success

Happy Learning 🙂

Download Example