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. When you use Thymeleaf, this folder is src/main/resources/templates/error. The file name can be one of these:
- The exact status code, for example
404.html.
- A series pattern:
4xx.html (client errors) or 5xx.html (server errors). Other patterns like 3xx are not 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.html).
- A page with the series pattern (for example
error/4xx.html).
- The global error page (
error.html).
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.
The same rules work for JSP pages. See our JSP version of this tutorial if you need it. This tutorial uses Thymeleaf, which is the better choice for new projects. It also runs as a normal executable JAR, so we do not need WAR packaging.
Example
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.
Mapping Status Codes to Thymeleaf Pages
src/main/resources/templates/error/5xx.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>5xx Error</title>
<style>
table td {
vertical-align: top;
border: solid 1px #888;
padding: 10px;
}
</style>
</head>
<body>
<h1>My 5xx Error Page</h1>
<table>
<tr>
<td>Date</td>
<td th:text="${timestamp}"></td>
</tr>
<tr>
<td>Error</td>
<td th:text="${error}"></td>
</tr>
<tr>
<td>Status</td>
<td th:text="${status}"></td>
</tr>
<tr>
<td>Message</td>
<td th:text="${message}"></td>
</tr>
<tr>
<td>Exception</td>
<td th:text="${exception}"></td>
</tr>
<tr>
<td>Path</td>
<td th:text="${path}"></td>
</tr>
<tr>
<td>Trace</td>
<td><pre th:text="${trace}"></pre></td>
</tr>
</table>
</body>
</html>
The page above is used for all status codes in the 5xx series, for example 500 and 501. It shows all the error attributes (timestamp, error, status, message, exception, path and trace). Not every attribute always has a value. Thymeleaf's th:text escapes the values, so they are safe to show in the page.
Let's also add a page for 404 (Not Found):
src/main/resources/templates/error/404.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>404 Error</title>
<style>
table td {
vertical-align: top;
border: solid 1px #888;
padding: 10px;
}
</style>
</head>
<body>
<h1>My 404 Error Page</h1>
<table>
<tr>
<td>Date</td>
<td th:text="${timestamp}"></td>
</tr>
<tr>
<td>Error</td>
<td th:text="${error}"></td>
</tr>
<tr>
<td>Status</td>
<td th:text="${status}"></td>
</tr>
<tr>
<td>Message</td>
<td th:text="${message}"></td>
</tr>
<tr>
<td>Exception</td>
<td th:text="${exception}"></td>
</tr>
<tr>
<td>Path</td>
<td th:text="${path}"></td>
</tr>
<tr>
<td>Trace</td>
<td><pre th:text="${trace}"></pre></td>
</tr>
</table>
</body>
</html>
Now let's add a global error page that is not tied to any status code (see our previous tutorial for details):
src/main/resources/templates/error.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Error</title>
<style>
table td {
vertical-align: top;
border: solid 1px #888;
padding: 10px;
}
</style>
</head>
<body>
<h1>My Custom Global Error Page</h1>
<table>
<tr>
<td>Date</td>
<td th:text="${timestamp}"></td>
</tr>
<tr>
<td>Error</td>
<td th:text="${error}"></td>
</tr>
<tr>
<td>Status</td>
<td th:text="${status}"></td>
</tr>
<tr>
<td>Message</td>
<td th:text="${message}"></td>
</tr>
<tr>
<td>Exception</td>
<td th:text="${exception}"></td>
</tr>
<tr>
<td>Path</td>
<td th:text="${path}"></td>
</tr>
<tr>
<td>Trace</td>
<td><pre th:text="${trace}"></pre></td>
</tr>
</table>
</body>
</html>
The URI /admin throws an exception with status code 403. We did not add 403.html or 4xx.html, so Spring Boot uses the global error page (error.html) in this case.
Spring Boot application.properties file
src/main/resources/application.propertiesspring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
spring.web.error.whitelabel.enabled=false
spring.web.error.include-stacktrace=always
spring.web.error.include-exception=true
spring.web.error.include-message=always
Here is what the properties do:
server.error.whitelabel.enabled=false turns off the default Whitelabel error page, so our error.html can be used as the global error page.
server.error.include-message, server.error.include-exception and server.error.include-stacktrace decide if the message, the exception and the stack trace are available to our pages.
Main class
package com.logicbig.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class);
}
}
Running the example
To run Spring Boot application you can run your main class from IDE, or execute it from the command line using your build tool: - Maven:
mvn spring-boot:run
- Gradle:
gradle bootRun
After running the Spring Boot main class, let's access the endpoint via a browser and curl.
Opening http://localhost:8080/app-info calls handleAppInfoRequest(), which throws our custom exception. The status code is 501, so 5xx.html is used:
Browsers send Accept: text/html for your Thymeleaf error page, while curl defaults to Accept: */*, returning JSON:
Curl Output $ curl -s "http://localhost:8080/app-info" {"timestamp":"2026-12-04T14:52:32.863Z","status":501,"error":"Not Implemented","exception":"com.logicbig.example.NotYetImplemented","trace":"com.logicbig.example.NotYetImplemented: The request page is not yet implemented\r\n\tat com.logicbig.example.MyController.handleAppInfoRequest(MyController.java:17)\r\n\tat java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)\r\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:565)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:258)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:190)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:934)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:853)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:86)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:866)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1003)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:892)\r\n\tat jakarta.servlet.http.HttpServlet.service(HttpServlet.java:622)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:874)\r\n\tat jakarta.servlet.http.HttpServlet.service(HttpServlet.java:710)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:128)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:199)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:165)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:77)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:113)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:83)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:72)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:397)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:903)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1778)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)\r\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:946)\r\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:480)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:57)\r\n\tat java.base/java.lang.Thread.run(Thread.java:1474)\r\n","message":"The request page is not yet implemented","path":"/app-info"}
Opening an unmapped URL, for example http://localhost:8080/other, returns 404, so 404.html is used:
Curl Output $ curl -s "http://localhost:8080/other" {"timestamp":"2026-12-04T14:52:23.283Z","status":404,"error":"Not Found","exception":"org.springframework.web.servlet.resource.NoResourceFoundException","trace":"org.springframework.web.servlet.resource.NoResourceFoundException: No static resource other for request '/other'.\r\n\tat org.springframework.web.servlet.resource.ResourceHttpRequestHandler.handleRequest(ResourceHttpRequestHandler.java:527)\r\n\tat org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:50)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:866)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1003)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:892)\r\n\tat jakarta.servlet.http.HttpServlet.service(HttpServlet.java:622)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:874)\r\n\tat jakarta.servlet.http.HttpServlet.service(HttpServlet.java:710)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:128)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:199)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:165)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:77)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:113)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:83)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:72)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:397)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:903)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1778)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)\r\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:946)\r\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:480)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:57)\r\n\tat java.base/java.lang.Thread.run(Thread.java:1474)\r\n","message":"No static resource other.","path":"/other"}
Opening http://localhost:8080/ calls handleRequest(), which throws a RuntimeException. The status code is 500 (Internal Server Error), so 5xx.html is used:
Curl Output $ curl -s "http://localhost:8080/" {"timestamp":"2026-12-04T14:52:08.003Z","status":500,"error":"Internal Server Error","exception":"java.lang.RuntimeException","trace":"java.lang.RuntimeException: test exception\r\n\tat com.logicbig.example.MyController.handleRequest(MyController.java:12)\r\n\tat java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)\r\n\tat java.base/java.lang.reflect.Method.invoke(Method.java:565)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:258)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:190)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:934)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:853)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:86)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:866)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1003)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:892)\r\n\tat jakarta.servlet.http.HttpServlet.service(HttpServlet.java:622)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:874)\r\n\tat jakarta.servlet.http.HttpServlet.service(HttpServlet.java:710)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:128)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:199)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:107)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:165)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:77)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:113)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:83)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:72)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:397)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:903)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1778)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)\r\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:946)\r\n\tat org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:480)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:57)\r\n\tat java.base/java.lang.Thread.run(Thread.java:1474)\r\n","message":"test exception","path":"/"}
Opening http://localhost:8080/admin returns 403. We did not add 403.html or 4xx.html, so Spring Boot falls back to our global error.html page:
Curl Output $ curl -s "http://localhost:8080/admin" {"timestamp":"2026-12-04T11:12:44.064+0000","status":403,"error":"Forbidden","exception":"com.logicbig.example.ForbiddenException","message":"The requested page is forbidden","trace":"com.logicbig.example.ForbiddenException: The requested page is forbidden\r\n\tat com.logicbig.example.MyController.handleAdminRequest(MyController.java:23)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:498)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)\r\n\tat org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:870)\r\n\tat org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:776)\r\n\tat org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:870)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:635)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:742)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\r\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)\r\n\tat org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)\r\n\tat org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)\r\n\tat org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)\r\n\tat org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)\r\n\tat org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\r\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\r\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\r\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\r\n\tat java.lang.Thread.run(Thread.java:748)\r\n","path":"/admin"}
Notes
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.html 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.html 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.html 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");
}
Example ProjectDependencies and Technologies Used: - Spring Boot 4.2.0
Corresponding Spring Framework Version 7.1.0 Version Compatibility: 2.0.0.RELEASE - 4.2.0
- 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))
- spring-boot-starter-thymeleaf (Starter for using Thymeleaf)
- JDK 25
|