When you run the spring boot application in the sprint tool suite for the first time, you may encounter a Whitelabel error page error like below even if there are no coding or logic errors in the source code. You can also find there are no error messages logged out and your spring source code can not enter debug mode. This article will tell you how to resolve this issue.
Below is the Whitelabel error page error text.
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback Mon Nov 19 20:50:28 CST 2021 There was an unexpected error (type=Not Found. status=404). No message available
1. Resolve Whitelabel Error Page.
- If you set up the example follow the spring boot hello world example correctly, and the source code does not have any error. This is mainly because your spring boot application’s project files structure is not correct.
1.1 Correct Spring Boot Application Project Files Structure.
- As we know every spring boot application has a main application class that will be executed to initialize the spring boot application.
- And this spring boot application main class should be annotated with
@SpringBootApplication
annotation. - The
@SpringBootApplication
annotation is the combination of the annotations@Configuration, @ComponentScan and @EnableAutoConfiguration
. @ComponentScan
can configure the base package to scan the spring components and@EnableAutoConfiguration
annotation can enable bean auto-configure in the application.- If there are more classes like the JPA class placed in a different(child) package than the spring boot main class package, so when you use the
@SpringBootApplication
annotation, you can not specify the base package value. - So if you use
@SpringBootApplication
annotation in your spring boot main class, you must place the main class in root package as bellow to avoid Whitelabel error page. - In the below spring boot example SpringBootWebMvcApplication.java is the main class. The root package is com.dev2qa.example, to avoid the Whitelabel error page, save the main class file in the root package.
C:\WORKSPACE\WORK\DEV2QA.COM-EXAMPLE-CODE\SPRINGBOOT\SPRINGBOOTWEBMVC │ .gitignore │ mvnw │ mvnw.cmd │ pom.xml │ ├───.mvn │ └───wrapper │ maven-wrapper.jar │ maven-wrapper.properties │ └───src ├───main │ ├───java │ │ └───com │ │ └───dev2qa │ │ └───example │ │ │ SpringBootWebMvcApplication.java │ │ │ │ │ ├───constant │ │ │ ConstantVariable.java │ │ │ │ │ ├───controller │ │ │ CustomErrorController.java │ │ │ EmployeeController.java │ │ │ │ │ ├───model │ │ │ Employee.java │ │ │ │ │ └───repository │ │ EmployeeRepository.java │ │ │ └───resources │ │ application.properties │ │ │ └───templates │ addEmployee.html │ error-500.html │ error.html │ listEmployee.html
- Below is the content of the SpringBootWebMvcApplication.java file. You can see we use
@SpringBootApplication
annotation for the main class.package com.dev2qa.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootWebMvcApplication { public static void main(String[] args) { /* Responsible for launching the boot application. */ SpringApplication.run(SpringBootWebMvcApplication.class, args); } }
1.2 Use @ComponentScan Annotation And Specify Base Package.
- If you do not want to place the spring boot main class in the spring boot app root package, you can use
@Configuration, @ComponentScan and @EnableAutoConfiguration
annotation to replace the@SpringBootApplication
annotation, and then specify the base package in the@ComponentScan
annotation. - In the above example we place the spring boot main class SpringBootWebMvcApplication in com.dev2qa.example.mvc package and the base package is com.dev2qa.example. So to avoid the Whitelabel error page, we should use
@ComponentScan
annotation and set the base packages in it.package com.dev2qa.example.mvc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages = {"com.dev2qa.example"}) @EnableAutoConfiguration public class SpringBootWebMvcApplication { public static void main(String[] args) { /* Responsible for launching the boot application. */ SpringApplication.run(SpringBootWebMvcApplication.class, args); } }
How do I fix this error:Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Feb 20 15:21:13 SAST 2024
There was an unexpected error (type=Internal Server Error, status=500).
Failed to download tax certificate 2023 for entity 1054979529