Spring Boot Resolve Whitelabel Error Page Example

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.

  1. 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.

  1. As we know every spring boot application has a main application class that will be executed to initialize the spring boot application.
  2. And this spring boot application main class should be annotated with @SpringBootApplication annotation.
  3. The @SpringBootApplication annotation is the combination of the annotations @Configuration, @ComponentScan and @EnableAutoConfiguration.
  4. @ComponentScan can configure the base package to scan the spring components and@EnableAutoConfiguration annotation can enable bean auto-configure in the application.
  5. 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@SpringBootApplicationannotation, you can not specify the base package value.
  6. 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.
  7. 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.
    │   .gitignore
    │   mvnw
    │   mvnw.cmd
    │   pom.xml
    │   └───wrapper
    │           maven-wrapper.jar
    │           maven-wrapper.properties
        │   ├───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
  8. 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;
    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.

  1. 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.
  2. 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;
    @ComponentScan(basePackages = {"com.dev2qa.example"})
    public class SpringBootWebMvcApplication {
       public static void main(String[] args) {
          /* Responsible for launching the boot application. */
          SpringApplication.run(SpringBootWebMvcApplication.class, args);
0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x