Spring Boot can show a custom error page for a specific HTTP status code. To do this, add a file named after the status code to the error folder. The error page can be a static HTML file (placed in any static resource folder) or a page made with a template engine, such as JSP or Thymeleaf.
The file name can be one of these:
The exact status code, for example 404.jsp or 404.html.
A series pattern, for example 4xx.jsp, 5xx.jsp or 5xx.html. Only 4xx (client errors) and 5xx (server errors) are supported, because error pages are only used for errors.
When an error happens, Spring Boot looks for a page in this order:
A page with the exact status code (for example error/404.jsp).
A page with the series pattern (for example error/4xx.jsp).
The global error page (for example error.jsp).
This works because Spring Boot registers an error endpoint at /error. When the application reports an error (by calling response.sendError(), or by letting an exception escape from a controller), the servlet container forwards the request to /error. Spring Boot then picks the best matching page from the list above.
In the following example, we will map status codes to JSP pages.
plugins {
java
id("org.springframework.boot") version "4.2.0"
id("io.spring.dependency-management") version "1.1.7"
}
println(">>> EXECUTING SCRIPT: build.gradle.kts (Kotlin DSL)")
group = "com.logicbig.example"
version = "1.0-SNAPSHOT"
java {
sourceCompatibility = JavaVersion.VERSION_25
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation ("org.apache.tomcat.embed:tomcat-embed-jasper")
implementation ("jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api")
}
plugins {
id 'java'
id 'org.springframework.boot' version '4.2.0'
id 'io.spring.dependency-management' version '1.1.7'
}
println ">>> EXECUTING SCRIPT: build.gradle (Groovy DSL)"
group = 'com.logicbig.example'
version = '1.0-SNAPSHOT'
java {
sourceCompatibility = JavaVersion.VERSION_25
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper'
implementation 'jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api'
}
Our JSP pages only use Expression Language ( ) and no JSTL tags, so we do not need a JSTL dependency.
Creating a controller
package com.logicbig.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@GetMapping("/")
public void handleRequest() {
throw new RuntimeException("test exception");
}
@GetMapping("/app-info")
public void handleAppInfoRequest() throws NotYetImplemented {
throw new NotYetImplemented(
"The request page is not yet implemented");
}
@RequestMapping("/admin")
public void handleAdminRequest() throws ForbiddenException {
throw new ForbiddenException(
"The requested page is forbidden");
}
}
The method handleRequest() throws a RuntimeException. This returns the status code 500 (Internal Server Error). The method handleAppInfoRequest() throws a custom exception. Let's bind that exception to a specific status code.
Specifying a Status Code on a Custom Exception
package com.logicbig.example;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public class NotYetImplemented extends Exception {
public NotYetImplemented(String message) {
super(message);
}
}
NOT_IMPLEMENTED means the response status code will be 501.
The method handleAdminRequest() throws another custom exception. Let's bind that exception to a different status code.
package com.logicbig.example;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.FORBIDDEN)
public class ForbiddenException extends Exception {
public ForbiddenException(String message) {
super(message);
}
}
FORBIDDEN means the response status code will be 403.
When @ResponseStatus is placed on an exception class, Spring MVC calls response.sendError() with that status code. This is what makes the request go to the /error endpoint and use our error pages.
The page above is used for all status codes in the 5xx series, for example 500 and 501.
Let's also add a page for 404 (Not Found). Both pages show all the error attributes (timestamp, error, status, message, exception and trace). Not every attribute always has a value.
The URI /admin throws an exception with status code 403. We did not add 403.jsp or 4xx.jsp, so Spring Boot uses the global error page (error.jsp) in this case.
Opening http://localhost:8080/ calls handleRequest(), which throws a RuntimeException. The status code is 500 (Internal Server Error), so 5xx.jsp is used:
An error page is used only when the application reports an error with response.sendError(), or when an exception is not handled. If the code only sets a status code, the browser gets that status code, but Spring Boot does not use any error page. The next three examples only set the status code.
First, @ResponseStatus on a controller method:
@RequestMapping("/some-info")
@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public void handleSomeInfoRequest() {
}
This returns status code 501 with an empty body. It does not use our 5xx.jsp page.
Second, @ResponseStatus on a controller method that throws an exception:
@RequestMapping("/some-info")
@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public void handleSomeInfoRequest() {
throw new RuntimeException("not yet implemented");
}
This always returns 500 (Internal Server Error), not 501. The @ResponseStatus on the method is ignored when an exception is thrown. The 500 status code uses our 5xx.jsp page.
Third, returning a ResponseEntity:
@RequestMapping("/some-info")
public ResponseEntity<Void> handleSomeInfoRequest() {
return ResponseEntity.status(HttpStatus.NOT_IMPLEMENTED).build();
}
This returns 501 with an empty body. It does not use our 5xx.jsp page either.
If you want an error page for a specific status code without creating a custom exception class, throw a ResponseStatusException. It calls response.sendError() for you, so the matching error page is used:
@RequestMapping("/some-info")
public void handleSomeInfoRequest() {
throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "not yet implemented");
}
Another option is to put a reason on the annotation, for example @ResponseStatus(code = HttpStatus.NOT_IMPLEMENTED, reason = "not yet implemented"). When a reason is given, Spring MVC also calls response.sendError().
Example Project
Dependencies and Technologies Used:
Spring Boot 4.2.0 Corresponding Spring Framework Version 7.1.0 Version Compatibility: 2.0.0.RELEASE - 4.2.0Version List
×
Version compatibilities of spring-boot-starter-parent with this example:
2.0.0.RELEASE
2.0.1.RELEASE
2.0.2.RELEASE
2.0.3.RELEASE
2.0.4.RELEASE
2.0.5.RELEASE
2.0.6.RELEASE
2.0.7.RELEASE
2.0.8.RELEASE
2.0.9.RELEASE
2.1.0.RELEASE
2.1.1.RELEASE
2.1.2.RELEASE
2.1.3.RELEASE
2.1.4.RELEASE
2.1.5.RELEASE
2.1.6.RELEASE
2.1.7.RELEASE
2.1.8.RELEASE
2.1.9.RELEASE
2.1.10.RELEASE
2.1.11.RELEASE
2.1.12.RELEASE
2.1.13.RELEASE
2.1.14.RELEASE
2.1.15.RELEASE
2.1.16.RELEASE
2.1.17.RELEASE
2.1.18.RELEASE
2.2.0.RELEASE
2.2.1.RELEASE
2.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
3.4.1
3.4.2
3.4.3
3.4.4
3.4.5
3.4.6
3.4.7
3.4.8
3.4.9
3.4.10
3.4.11
3.4.12
3.4.13
3.5.0
3.5.1
3.5.2
3.5.3
3.5.4
3.5.5
3.5.6
3.5.7
3.5.8
3.5.9
3.5.10
3.5.11
3.5.12
3.5.13
3.5.14
3.5.15
3.5.16
4.0.0
4.0.1
4.0.2
4.0.3
4.0.4
4.0.5
4.0.6
4.0.7
4.0.8
4.0.9
4.0.10
4.1.0
4.1.1
4.1.2
4.1.3
4.2.0
Versions in green have been tested.
spring-boot-starter-web (Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container (deprecated in favor of spring-boot-starter-webmvc))