membersound
membersound

Reputation: 86747

How to replace Spring Boot's "Whitelabel Error Page" with a blank page?

On any exceptions, by default Spring Boot routes to /error, which generates an error HTML page:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Oct 31 16:01:01 CET 2018
There was an unexpected error (type=Not Found, status=404)

Question: how can I instead show a blank page by default for a certain endpoint only?

There is a property server.error.whitelabel.enabled=false, but that disables the error handling entirely, so that the webservers default error page is shown (e.g. on Tomcat a 404 error page).

That's not what I want. I'd just like to show a blank plain page. Or so to say: an empty response body. But how?

Because, for development and testing the "Whitelabel Error Page" is fine. But in production I'd like to hide any exception details entirely.

Upvotes: 5

Views: 5344

Answers (2)

membersound
membersound

Reputation: 86747

I just discovered that this "Whitelabel Error Page" is only shown inside a web browser requesting a text/html content type.

If a Spring REST webservice is accessed from a native client using just application/json, the error is translated to JSON automatically.

In case one would really want to show a blank page for web browsers, one could just override the BasicErrorController#errorHtml() bean method somehow.

Upvotes: 3

Joris Schellekens
Joris Schellekens

Reputation: 9022

I think this tutorial describes the way to go: https://www.baeldung.com/spring-boot-custom-error-page

Essentially you would do something like this:

@Controller
public class MyErrorController implements org.springframework.boot.web.servlet.error.ErrorController {

    @RequestMapping("/error")
    public String handleError() {
        //do something like logging
        return "error";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

And then you'd make sure the default behaviour is turned off.

@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
@SpringBootApplication
public class Application {

    public static void main(String... args) {
        SpringApplication.run(Application.class, args);
    }

}

Upvotes: 4

Related Questions