DEV Community

send2abhishek
send2abhishek

Posted on

How to customise white-label error in spring boot

Hi Guys in this post I am going to explain how to override the white-label error page in springboot application.

Follow below steps to achieve this -

  • Create a new class annotate with @ControllerAvice and extend it with ResponseEntityExceptionHandler (this is the central class that wish to provide centralized exception handling across all @RequestMapping methods through @ExceptionHandler methods).
@ControllerAdvice
public class GlobalExceptionHandler extends
ResponseEntityExceptionHandler {}
Enter fullscreen mode Exit fullscreen mode
  • As white label error produces because of various reasons but in this post I am going to focus only on when there is no handler attached to the request sent by the client. In this case due to spring auto configuration route wired to the spring default error route which as a result shows the white label error page. So now we know the root cause lets customise this behaviour.

  • Override the method handleNoHandlerFoundException from ResponseEntityExceptionHandler

 @Override
    protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

 String path = ((ServletWebRequest) request).getRequest().getRequestURI();

return new ResponseEntity<>(new ErrorResponse(status, ex.getLocalizedMessage(), path, ex.getMessage()), status);
    }
Enter fullscreen mode Exit fullscreen mode

In the above code simply I am returning a bean along with wiring the root messages and path.
Also I have used a custom pojo class to structure the information. Lets create this.

@Getter
@Setter
public class ErrorResponse {

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")
    private Date timestamp;
    private int code;
    private String status;
    private Object message;
    private String path;
    private String stackTrace;


    public ErrorResponse() {
        timestamp = new Date();
    }

    public ErrorResponse(HttpStatus httpStatus, Object message) {
        this();

        this.code = httpStatus.value();
        this.status = httpStatus.name();
        this.message = message;
    }

    public ErrorResponse(HttpStatus httpStatus, Object message, String path) {
        this(httpStatus, message);
        this.path = path;
    }

    public ErrorResponse(HttpStatus httpStatus, Object message, String path, String stackTrace) {
        this(httpStatus, message, path);
        this.stackTrace = stackTrace;
    }
}

Enter fullscreen mode Exit fullscreen mode

In above class I am using the getter and setter annotation from project lombok.

  • As the final step lets tell the spring boot that we have error handler controller defined and not to use the default one by specifying the below properties as below
spring.mvc.throw-exception-if-no-handler-found=true
spring.web.resources.add-mappings=false
Enter fullscreen mode Exit fullscreen mode

Please note this will work in spring boot version >=2.4 if version less 2.4 use below configuration

spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
Enter fullscreen mode Exit fullscreen mode

Now lets run this app. I am testing for /about route in my app its handler is not defined.

Image description

Thanks for the reading. Let me know your thoughts in comment section.

Top comments (0)