Java is a programming language with an infinite number of programs. No programmers have worked on all projects. When they try to work on a project for the first time, they end up with issues and errors. It a completely normal as when we work on a project, we get error warnings. You may encounter ‘This application has no explicit mapping for /error, so you are seeing this as a fallback’ error warning that makes you wonder how you land up in such a long error code.
Now that you have come here, you don’t need to worry as we are a helping hand for programmers and beginners. You can see the error that pops up and how it occurs. Let’s check out how you get the error warning
How the error shows up
When you try to start a Spring application, you get the page with the error. Have a look at it
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jun 30 10:43:58 CST 2020
There was an unexpected error (type=Not Found, status=404).
No message available
You can see the error on your browser session. The warning is the result of an error response for the request. The 404 error you see is because the server is unable to find the URL requested. You need to provide Spring Boot with your Whitelabel view.
Solutions to Fix ‘This application has no explicit mapping for /error, so you are seeing this as a fallback’ error
You can handle this exception in two ways
Solution 1 – Implementation of the ErrorController
To solve the error, you need to give your ErrorController implementation. You can use a controller to remove the Whitelabel page and show a static error message. Have a look at the code
@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
@ResponseBody
String error(HttpServletRequest request) {
return "<h1>Error occurred</h1>";
}
@Override
public String getErrorPath() {
return "/error";
}
}
It provides a mapping to remove the error. Meanwhile, it also provides a mapping to static HTML.
Solution 2 – Clean Up using Maven
If the error shows up because of switching to a new IDE from Maven. You need to clean up the Maven. When you are compiling, messed up plugins and files can cause the error. So, you need to clean it using the below code
$ mvn clean
Solution 3 – MVC Template
This is another way to fix ‘This application has no explicit mapping for /error, so you are seeing this as a fallback’ error. In the case, you are using the Thymeleaf type templating engine, then providing an error.html is a simple process.
The MVC view can have access to the errors and exceptions, and also render it, which makes it simpler for you. You need to ensure that your templating engine is in the correct place. You can have any templating engine of your choice. You have an option to use Spring Boot to compare the templating engines as it can help you decide. Use the below code to help you decide
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Once you are done, under ‘src/main/resources/templates/’, create error.html. Follow the code
!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>
Use the application.properties to add two values to show more details. Check the values added
server.error.include-exception=true
server.error.include-stacktrace=always
Conclusion
We have highlighted the solutions to fix ‘This application has no explicit mapping for /error, so you are seeing this as a fallback’ in an efficient way. You can choose either way to get rid of the error warning.
I wish you luck!
If you need any further assistance, feel free to comment below! We would be happy to help you!
Reference Source: https://www.codejava.net/frameworks/spring-boot/spring-boot-error-handling-guide