How to solve whitelabel error page when using springboot and jsp

1. Introduction

When using springboot mvc , you may encounter this error:

You visit: http://localhost:8080/ , it should ok, but the browser display an error page like this:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

2. Environments

  • SpringBoot 1.x and 2.x
  • Java 1.8+
  • Maven 3.3+

3. How to replay this error?

3.1 The project layout

I have a springboot project , just for test the springboot and mvc: 20180601_sberror2

It’s very simple.

3.2 The problem

when I run it, and visit http://localhost:8080, I got this: 20180601_sberror4

4. How to debug this problem?

Just search the keywords Whitelabel Error Page from the spring project at github , you can get to the ErrorMvcAutoConfiguration, which has this code:

	@Configuration(proxyBeanMethods = false)
	@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled",matchIfMissing = true) //notice this line
	@Conditional(ErrorTemplateMissingCondition.class)
	protected static class WhitelabelErrorViewConfiguration {...

Notice the @ConditionalOnProperty annotation, it means that:

  • If server.error.whitelabel.enabled is not defined,condition match
  • Else if server.error.whitelabel.enabled is true,condition match

5. How to solve this problem?

5.1 Disable whitelabel error page with property

According to the above inspection of the spring source code, the solution to hide the Whitelabel Error Page is :

In your src/main/resources/application.properties, just add this line:

server.error.whitelabel.enabled=false

5.2 Disable whitelabel error page with annotation

You can see that the WhitelabelErrorViewConfiguration class is loaded by ErrorMvcAutoConfiguration, which is a @Configuration class, so ,you can exclude it in your springboot main class like this:

@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

6. Summary

The WhiteLabel Error Page issue is a nightmare for newbies of springboot. After reading this article, you can find that the key point is to find why spring show this page and where the code is, and then the solution is simple.