I have a custom exception class annotated to return a given HttpStatus:
@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Invalid parameter")
public class BadRequestException extends RuntimeException
{
public BadRequestException(String msg)
{
super(msg);
}
}
This works when I throw a BadRequestException from my controller but the reason is always “Invalid parameter” of course. Is there a way to set the returned reason in this class? I’d like to pass a string to be used as the reason.
Thanks!
You can use
HttpServletResponse‘ssendErrorfunction to achieve that.Here is an example of how to use it: