🏠Spring BootThis application has no explicit mapping for /error

This application has no explicit mapping for /error

The “this application has no explicit mapping for /error” view is a common Spring Boot-related issue. If you are new to Spring Boot, you might have seen this error already. If you are looking for how to solve this issue, This is what you should do.

What and why?

You would usually see this error in a Whitelabel Error Page on a browser session. For example, take a look at the following screenshot.

This application has no explicit mapping for error, so you are seeing this as a fallback.

One would usually see such errors when there is an error response for the request. In this case, the server cannot find the requested URL (HTTP 404). But the reason why the error message also involves an explicit mapping for /error is due to how the Whitelabel Error page works.

Basically, Spring Boot application has an ErrorMvcAutoConfiguration for handing controller errors. For this,

  • Spring Boot creates a BasicErrorController which can handle each exceptions
  • And also, it creates a static “/error” view which is the “Whitelabel Error Page”

In other words, Spring Boot wants you to provide your own whitelabel view.

Solutions

The solution for this error is to provide a /error mapping. And you can do that in two ways.

Solution 1: Implement the ErrorController interface

You could provide your own implementation of ErrorController. For example, the below controller will show a static error message instead of a whitelabel page.

@Controller
public class CustomErrorController implements ErrorController {


  @RequestMapping("/error")
  @ResponseBody
  String error(HttpServletRequest request) {
    return "<h1>Error occurred</h1>";
  }

  @Override
  public String getErrorPath() {
    return "/error";
  }
}
Code language: PHP (php)

This way you are providing a mapping for /error as well as a static HTML as a response. Take a look at BasicErrorController if you want to make this response a bit more dynamic.

Solution 2: Provide an MVC template for /error

This approach is probably the best way to handle the ” no explicit mapping for /error ” message. If you are using a templating engine like Thymeleaf, you could simply provide an error.html.

As the MVC views have access to the exceptions and error messages, it is easier to define a template and let the templating engine do the rendering for you. To achieve this, make sure you have a templating engine in place. For this example, I’m using Thymeleaf. You could however use any template engine of your choice. We have a great comparison of template engines in Spring Boot if you need help deciding.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>Code language: HTML, XML (xml)

Then create “error.html” under “src/main/resources/templates/” with the following content or similar.

<!doctype html>
<html lang="en">
<head>
  <title th:text="${message}"></title>
</head>
<body>
<table border="1">
  <tr><td>Error Message</td><td th:text="${message}"></td></tr>
  <tr><td>Status Code</td><td th:text="${status}"></td></tr>
  <tr><td>Exception</td><td th:text="${exception}"></td></tr>
  <tr><td>Stacktrace</td><td><pre th:text="${trace}"></pre></td></tr>
  <tr><td>Binding Errors</td><td th:text="${errors}"></td></tr>
</table>
</body>
</html>
Code language: HTML, XML (xml)

For the sake of showing more details, I have added the following two values to the application.properties.

server.error.include-exception=true
server.error.include-stacktrace=alwaysCode language: PHP (php)
Result of a custom /error mapping

With some CSS magic, you could get a nice error page for yourself.

custom error.html with CSS

Summary

To conclude, we learned why “This application has no explicit mapping for error” message appears and how to solve them in two different ways.

Related

Similar Posts