I have a @ControllerAdvice extending ResponseEntityExceptionHandler as an attempt for me to control standard response for any exception raised with in the API call workflow.
Without the Controller advice. I get HTML based generic response generated by spring with correct response headers. But when I add my @ControllerAdvice, Spring doesn't response with generic error body. The body is empty with correct response headers
@Override
protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
String erroMessage = "Required Parameter: '"+ex.getParameterName()+"' was not available in the request.";
TrsApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, erroMessage, ex, ApiErrorCode.INVALID_REQUEST);
return buildResponseEntity(apiError);
}
So, now in case of a required parameter missing in the request, the flow beautifully trigger my overridden implementation and responds with JSON payload describing the error. But, in case of any other exception like HttpMediaTypeNotAcceptableException, spring is responding with empty body.
Before I added my advice, spring was responding with generic error response. I am new to spring boot ecosystem. Need help in understanding if this is an expected behavior of if there is a better approach of achieving centralized error handling.