. Advertisement .
..3..
. Advertisement .
..4..
You may see this page after starting a Spring application:
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
What is it? And how can you remove this page? Read on to find out.
This application has no explicit mapping for /error, so you are seeing this as a fallback.
What Does This Page Tell Us?
This is a generic error page that this framework uses when no custom page has been defined. It is just a notification of something wrong under the hood without indicating what specific error has occurred.
Luckily, many common mistakes that lead to this error page have been well documented. You can consider them one by one to see which could solve your problem.
Location Of The Main Application Class
Spring’s official documentation recommends placing the main application class above other classes.
The reason is that this main class typically contains @SpringBootApplication. And this annotation is a wrap-up of other important annotations that the application needs to run.
When you create a Spring application, the framework needs to know where to look for the necessary components. Scanning is a critical step because, without it, Spring can’t detect all the annotated components that it should manage.
The @SpringBootApplication annotation is the main method of doing this. In an implicit manner, it defines a starting place for Spring to look for certain components.
When using this annotation, the best practice is to place the main application class above the components classes. The reason is that by default, Spring only automatically scans the current package as well as its sub-packages while looking for components.
For instance, @SpringBootApplication, in an implicit manner, defines a starting place for the application to look for certain objects. Having the main class in the root package doesn’t just allow the program to scan items but also applies it to only your specific project.
If you have a package named com.ittutoria.spring.boot, then a controller package like this won’t work.
package com.ittutoria.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
@RequestMapping("/")
public String hello_world(){
return "Hello World!";
}
}
This snippet will produce the Whitelabel Error Page instead because it doesn’t fall under the main class (com.ittutoria.spring.boot). A change in the package location should avoid it.
package com.ittutoria.spring.boot.controller;
It results in the correct layout for the above example. It makes sure Spring scans the classes in the CustomerController.java file as well.
com
+- example
+- your_application
+- YourApplication.java
|
+- customer
| +- Customer.java
| +- CustomerController.java
Remember that the YourApplication.java file should have the @SpringBootApplication along with the main method:
@SpringBootApplication
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
Additionally, @ComponentScan can be used to explicitly define the scanning behaviors. Most developers use @ComponentScan along with @Configuration to describe exactly which packages Spring should scan.
After all, @SpringBootApplication is just a convenient shortcut provided by Spring Boot to annotate the main class with @ComponentScan, @EnableAutoConfiguration, and @Configuration at the same time.
@ComponentScan("com.ittutoria.controller")
Fresh Compilation
Many developers have also reported that this error occurs when they switch to a new IDE from Maven. Previous compilations may mess up with the transition as they leave conflicting files, especially for certain plugins.
A clean-up by Maven should remove these unnecessary files:
$ mvn clean
This practice is also recommended every time you need to compile your Spring application.
Conclusion
If you see the “This application has no explicit mapping for /error, so you are seeing this as a fallback” message, there is a high chance that your project structure is incorrect. The main class should be above, not under, other components of the application.
Leave a comment