I have a custom exception class annotated to return a given HttpStatus
:
@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason=\"Invalid parameter\")
The easiest way to just set the response.setStatus()
. Easy and clean, you can change it to any status you want just instead of ex.getStatusCode()
add your code.
The return type is also of your choice, I'm using String b/c displaying this later.
By the way, the sendError
is not a good idea, because JBoss for instance is adding a lot of HTML to the response.
@ExceptionHandler(CommunicationException.class)
@ResponseBody()
public String handleCommunicationException(CommunicationException ex, HttpServletResponse response) throws IOException{
response.setStatus(ex.getStatusCode());
return ex.getStatusMessage();
}