Spring Boot Resolve Whitelabel Error Page Example

When you run spring boot application in 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 error in 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.

spring boot whitelabel error page

1. Resolve Whitelabel Error Page.

If you setup the example follow spring boot hello world example correctly, and the source code do not has any error. This is mainly because of 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 which will be executed to initialize spring boot application.

And this spring boot application main class should be annotated with @SpringBootApplication annotation.

The @SpringBootApplication annotation is the combination of @Configuration, @ComponentScan and @EnableAutoConfiguration annotation.

@ComponentScan can configure the base package to scan spring component and@EnableAutoConfiguration can enable bean auto configure in the application.

If there are more classes like JPA class placed in different(child) package than spring boot main class package, so when you use @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 below spring boot example SpringBootWebMvcApplication.java is the main class. The root package is  com.dev2qa.example, to avoid whitelabel error page, save the main class file in the root package.

READ :   Spring Boot Integrates slf4j and logback

correct spring boot application files structure

Below is the content of 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.

place spring boot application main class in child package than root package

In 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 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);
   }
}
(Visited 33 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

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