Disable Whitelabel Error Page in Spring Boot



Disable Whitelabel Error Page in Spring Boot | ninjasquad

In this article, we’ll learn how to disable default Whitelabel Error Page in Spring Boot.

Overview

Whitelabel error page appears when you hit a wrong or unknown request url, which is generated by Spring Boot.

We can use any one of these two properties to disable this default behavior:-

server.error.whitelabel.enabled = false

spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

## For Spring Boot < 2.0
spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration

However once disabled, the error page shown is generated by underlying web application server such as Tomcat, Undertow, etc. instead of Spring Boot.

Disable Whitelabel Server Property

You can disable the Whitelabel error page by setting the server.error.whitelabel.enabled property to false. This can be done from .properties file, .yml file, or through command-line parameters as follows:-

## application.properties
server.error.whitelabel.enabled = false
## application.yml
server:
  error:
    whitelabel:
      enabled: false
$ java -jar -Dserver.error.whitelabel.enabled=false spring-boot-app-1.0.jar
$ java -jar spring-boot-app-1.0.jar --server.error.whitelabel.enabled=false

Exclude ErrorMvcAutoConfiguration

Another way to disable it via excluding the ErrorMvcAutoConfiguration from auto configuration. This can be done easily from .properties file or .yml file

## application.properties
spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration
## application.yml
spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

Alternatively, you can also use @EnableAutoConfiguration annotation to exclude it.

import org.springframework.context.annotation.Configuration;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
...
@Configuration
@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
public static MainApp { ... }
For Spring Boot < 2.0

Please note that for Spring Boot < 2.0, the ErrorMvcAutoConfiguration class is located in package org.springframework.boot.autoconfigure.web.

## application.properties
spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration

## application.yml
spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration



Source: Internet

Leave a Comment

We are offering free coding tuts

X