Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3637

How to modify default spring boot not found url exception with custom message

$
0
0

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 ?


Viewing all articles
Browse latest Browse all 3637

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>