I am trying to create a custom error page for invalid URL in SpringMvc (Spring-boot version 1.5.1).
In order to disable the default whitelabel error page I have:
application.properties
spring.thymeleaf.cache=false
server.error.whitelabel.enabled=false
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
My exception handler is:
RestResponseEntityExceptionHandler.java
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
public RestResponseEntityExceptionHandler() {
super();
}
@Override
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
logger.error("404 Status Code", ex);
final GenericResponse bodyOfResponse = new GenericResponse(messages.getMessage("No such page", null, request.getLocale()), "NoHandlerFound");
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
}
}
This works in principle. If I go to an invalid URL in the browser I get a JSON which looks like:
{"message":"No such page","error":"NoHandlerFound"}
Instead of the JSON response I would like to show a proper HTML view (similar to the whitelabel page). This should be a template where I can replace the "message" string. How do I go about rendering this view?