A common Spring Boot-related issue is the “this application has no explicit mapping for /error” view. If you are new to Spring Boot, you may have already encountered this error. If you’re wondering how to solve this problem, this is what you should do.
What Causes This Error?
Such errors are typically seen when there is an error response for the request. In this case, the server is unable to locate the requested URL (HTTP 404). However, because of the way the Whitelabel Error page works, the error message also includes an explicit mapping for /error.
- For handling controller errors, the Spring Boot application has an ErrorMvcAutoConfiguration class. As a result,
- Spring Boot generates a BasicErrorController that handles all exceptions.
- It also generates a static “/error” view, which serves as the “Whitelabel Error Page.”
- Spring Boot, in other words, wants you to provide your own whitelabel view.
Solutions To This Error
1. Implement ErrorController Interface
- You could provide your own ErrorController implementation. For example, the controller below will display a static error message rather than a whitelabel page.
- As a result, you’re providing a mapping for /error as well as a static HTML response. If you want to make this response more dynamic, look into BasicErrorController.
2. Provide An MVC Template For /Error
- This is most likely the best way to deal with the “no explicit mapping for /error” message.
- You could simply provide an error.html if you’re using a templating engine like Thymeleaf.
- Because MVC views can access exceptions and error messages, it is simpler to define a template and let the templating engine handle the rendering.
- Make sure you have a templating engine in place to accomplish this.
- Thymeleaf is used in this example. However, you could use any template engine of your choice.
- If you need help deciding, we have a great comparison of template engines in Spring Boot.
- Then, under “src/main/resources/templates/,” create “error.html” with the following or similar content.
- I’ve added the following two values to the application.properties to show more details.
- You could create a nice error page for yourself using CSS.
To summarize, we learned why the message “This application has no explicit mapping for error” appears and how to resolve it in two ways.
We hope these fixes do the job for you. If you have any queries/suggestions regarding the contents of this article, or in general, please let us know in the comments down below.