How to customise white-label error in spring boot 
Tags: java, springboot, handlewhitelabelerror
Published: 2022-01-02 15:13:09

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 {}
```

- 
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);
    }
```
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;
    }
}

```
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
```
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
```

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mejy3fogdrd4xkjy8u39.png)

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