0% found this document useful (0 votes)
52 views8 pages

Exception Handling SpringBoot

This document discusses the "This application has no explicit mapping for /error" error that can occur in Spring Boot applications. It explains that this error means there is no defined mechanism for handling unhandled exceptions. The document then provides two approaches for addressing this error - implementing global exception handling to display custom error pages, and creating an "error.html" page to handle 404 errors.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
52 views8 pages

Exception Handling SpringBoot

This document discusses the "This application has no explicit mapping for /error" error that can occur in Spring Boot applications. It explains that this error means there is no defined mechanism for handling unhandled exceptions. The document then provides two approaches for addressing this error - implementing global exception handling to display custom error pages, and creating an "error.html" page to handle 404 errors.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

Spring Boot is a popular framework for Java applications,

providing developers with a seamless development


experience. However, even with its ease of use,
developers might encounter certain challenges, especially
when it comes to error handling. One common issue that
beginners face is the error message “This application has
no explicit mapping for /error.” In this comprehensive
guide, we’ll explore the reasons behind this error, various
approaches to troubleshooting it, and best practices for
handling exceptions in Spring Boot. Whether you’re new
to Spring Boot or an experienced developer, this article
will equip you with the knowledge to handle this error
effectively.
Understanding “This Application Has No Explicit
Mapping for /error”
When you encounter the error message “This Application
Has No Explicit Mapping for /error” in your Spring Boot
application, it means that there is no defined mechanism
to handle unhandled exceptions or errors. In other words,
when an unexpected exception occurs, the application
doesn’t know how to handle it, resulting in the “/error”
message.
Another common reason for this error message is that
Spring Boot is unable to find a handler for a specific URL
or endpoint that has been requested.

Causes of the “/error” Message


Unhandled Exceptions: If your application encounters an
unhandled exception, Spring Boot will attempt to handle
it using its default error handling mechanism. However, if
there is no explicit mapping for handling such exceptions,
the “/error” message is triggered.
Misconfigured Routes: Improper configuration of routes
or endpoints can also lead to this error. If the requested
URL doesn’t match any defined route, the application will
attempt to handle it with the “/error” mapping.
Example:
In the below images, you will notice that the Whitelabel
Error Page is displayed in two kinds of scenarios:
1. Unhandled Exception: In this case, a 500 Internal Server
Error was thrown when an unexpected error was
encountered within the application but no custom
exception handler was present to handle that error.
2. No Request Handler: In this case, a 404 Not Found
Error was thrown when a request was received with an
invalid URL that does not exist within the application.

No Explicit Mapping Error HTTP 500 Internal Server


Error

No Explicit Mapping Error 404 Not Found


Approach 1:
Implementing Global Exception Handling
If you want to display different error pages or if you want
to send different error messages in JSON format for
different kinds of errors:
Step-1: Implement the global exception handler
Create a global exception handler using
Spring’s @ControllerAdvice and @ExceptionHandler ann
otations. This handler will catch any unhandled
exceptions and provide a meaningful response to the
client.
Step-2: Define multiple exception handler methods
Define multiple exception handlers each one handling a
specific kind of exception.
a. If you want to display the custom HTML page then the
return type of these exception handler methods should be
String. While returning the response, specify the template
name that must be displayed to the user. You can also set
the required attributes using Model and use them inside
the template to make the template dynamic as per your
requirement.
b. If you want to send the JSON response instead of an
HTML page then you can return the Java object
containing the required fields.
Example:
GlobalExceptionHandler.java
@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(NoHandlerFoundException.class)
public String handleNotFoundError(Model model) {
System.out.println("No handler found exception");
String errorMessage = "OOops! Something went
wrong - value passed via exception handler.";
model.addAttribute("errorMessage", errorMessage);
return "error"; // This will display the "error.html"
Thymeleaf template
}
@ExceptionHandler(CustomException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ER
ROR)
@ResponseBody
public ErrorResponse
handleCustomException(CustomException ex) {
// This will return the response in JSON format
return new ErrorResponse("An error occurred: " +
ex.getMessage());
}
}
Error.html
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Error Page</title>
<!-- Add your CSS styles and other assets here -->
</head>
<body>
<div>
<!-- Use Thymeleaf expression ${errorMessage} to
display the error message -->
<h1 th:text="${errorMessage}">OOops! Something
went wrong - default message.</h1>
<p>We apologize for the inconvenience. The requested
page is not available at the moment.</p>
<!-- You can add more helpful information or links to
guide users -->
</div>
</body>
</html>
When the CustomException is encountered, the JSON
response will be sent to the user in the case of
the browser as well as in the case of the Postman.
When NoHandlerFoundException is encountered,
the error.html template will be displayed to the user in the
case of the browser. In the case of the Postman, the
HTML page content will be returned to the user.

Second Approach:
in case of 404 errors, the JSON response was displayed in the browser as well as in the case of
the postman. If you want to display a custom HTML Error Page when users encounter unmapped
or erroneous URLs, this is how it can be done:

Step-1: Add Thymeleaf Dependency

Add Thymelead dependency in pom.xml of your application.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
pom.xml
Step-2: Create error.html Page
Create an error page named error.html in
the src/main/resources/templates directory:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Error Page</title>
<!-- Add your CSS styles and other assets here -->
</head>
<body>
<div>
<h1>Oops! Something went wrong</h1>
<p>We apologize for the inconvenience. The requested page is not
available at the moment.</p>
<!-- You can add more helpful information or links to guide users -->
</div>
</body>
</html>

You might also like