I need to return a customized error page for HTTP 404, but the code does not work. I already read the stackflow 1,2, 3 and different online + articles but my case is quite different with those or at least I can not figure out the issue.
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
For example, it is suggested to use an action name in web.xml to handle the exception, it might work but I think is not a good method of doing it.
I used following combinations:
1)
@Controller //for class
@ResponseStatus(value = HttpStatus.NOT_FOUND) //for method
2)
@Controller //for class
@ResponseStatus(value = HttpStatus.NOT_FOUND) //for method
@ExceptionHandler(ResourceNotFoundException.class) //for method
3)
@ControllerAdvice //for class
@ResponseStatus(value = HttpStatus.NOT_FOUND) //for method
3)
@ControllerAdvice //for class
@ResponseStatus(HttpStatus.NOT_FOUND) //for method
4)
@ControllerAdvice //for class
@ResponseStatus(value = HttpStatus.NOT_FOUND) //for method
@ExceptionHandler(ResourceNotFoundException.class) //for method
Code
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public String handleBadRequest(Exception exception) {
return "error404";
}
}