I'm trying to modify the default error message and status code for all non supported rest end points.
How to do it ?
My Customer error message
public class ApiErrorResponse { private int code; private String message; public ApiErrorResponse(int code, String message) { this.code = code; this.message = message; } public String getMessage() { return message; } public int getCode() { return code; }}
Custom error handling logic:
@ControllerAdvicepublic class ApiExceptionHandler { @ExceptionHandler({NoHandlerFoundException.class}) public ResponseEntity<ApiErrorResponse> handleNoHandlerFoundException( NoHandlerFoundException ex, HttpServletRequest httpServletRequest) { ApiErrorResponse apiErrorResponse = new ApiErrorResponse(404, "Resource not found"); return ResponseEntity.status(HttpStatus.NOT_FOUND).contentType(MediaType.APPLICATION_JSON).body(apiErrorResponse); }}
and now tried like curl --location 'localhost:8080/some/invalid/url'But it gives default 404 status code and error message. not custom message . What I'm missing here ?