schlingel
schlingel

Reputation: 1694

How to disable whitelabel error page in application.yaml (spring boot)

I have a legacy Java monolith backend, serving REST APIs (spring boot v1.5.9, gradle) in hand which I have to maintain from time to time. A request came to disable such an error page:

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

Wed Mar 10 13:41:12 CET 2021 There was an unexpected error (type=Internal Server Error, status=500)."

I tried to get rid of this message but most of the advices explain how to remove it in a "application.properties" file, which my project doesn't have at all. Instead, I have application.yaml files, where I added this configuration:

spring:
  autoconfigure:
    exclude:
      - org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration

But I still see the same error page.

Other advices tell me to do something on "main" class, which my project unfortunately doesn't have, either.

I also tried to implement ErrorController to get my own Error page but my controller is not registered at all. Standard project structure recommendations are not followed on this legacy app, so any link to example structures on spring boot documentations will not help me.

Any advice is appreciated to disable the whitelabel error page or implementing ErrorController.

Upvotes: 1

Views: 3188

Answers (1)

Giorgi Tsiklauri
Giorgi Tsiklauri

Reputation: 11120

If you want to disable default "Whitelabel Error Page", you can add:

server:
    //..optionally, some other properties
    error:
        whitelabel:
            enabled: false
    //..optionally, some other properties

in your application.yml.

However, if you want to override that page, instead of disabling it and displaying application server's default error page, you have few ways:

  1. Create your custom error.html page, place it under your templates folder (or wherever your view are being picked up from), and it will be automatically picked up by Spring Boot's default BasicErrorController as a generic error page, that is - for all HTTP error codes;

  2. You can be more specific and create different error pages for different HTTP error codes.
    Name your error file with the HTTP status code you want to handle and save it in resources\templates\error\. This way, you may have several error pages and they will be used for corresponding HTTP errors.
    404.html, for instance, will be returned when HTTP 404 happens.

I hope this helps.

Upvotes: 2

Related Questions