0

I haven't any mapping for several URLs for my Spring Boot App. For example this one: enter image description here

I have an ExceptionHadler:

    @RestControllerAdvice(annotations = RestController.class)
    public class RestExceptionHandler {

        @ExceptionHandler(NotFoundException.class)
        protected ResponseEntity<String> handleNotFoundException() {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new NotFoundException().getMessage());
        }
    }

And I want to handle that exception in my handler. NotFound presented here is my custom exception. Which is the class/type of the Whitelabel Error, and how I can handle it like I handle my custom NFE?

Maksim Rybalkin
  • 277
  • 2
  • 10

2 Answers2

2

This corresponds to the case where you're trying to call URL which is not mapped at all.

Controller Advice can help in cases where the specific controller itself produces an exception, but here, you even don't have a controller, therefor controller advice is irrelevant. Error Page is spring MVC's fallback for cases like this.

If you want to customize this error page, you can read this tutorial

You might also be interested to read my answer in this thread where I've described how does spring MVC work under the hood and when controller advice is applicable.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
1

If you are using Spring Boot with Thymeleaf, you can:

  1. Exclude the default page adding this line to the properties file spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
  2. Create your custom html error page under resources/template directory
  3. Create a class which implements ErrorController to manage also the type of the error you get and eventually add specific error message based on this.

You can find more details in this article on Baeldung, which does this procedure step by step.

A. Wolf
  • 1,309
  • 17
  • 39