I want to send a custom JSON response for 400, 401, 405, 403 and 500 errors in spring boot. For these errors spring boot sometimes sends an HTML response, sometimes an empty response body response and sometimes full-fledged response having timestamp, exception, message, etc. I want a single and consistent JSON response for all of these errors, like this
{
statusCode : 405,
message: "HTTP Method not allowed"
}
I have used @ControllerAdvice to send custom responses for a lot of exceptions. The problem I am facing with these 400, 401, 405, 403 and 500 errors is that I don't know which exceptions are thrown when these errors occur so that I can write @ExceptionHandlers for them. Even I don't know if it's possible this way or even if possible then it is the right way to implement this.
Is there any way to implement this in Spring boot?