I want to return HTTPStatus code dynamically like 400, 400, 404 etc as per the response object error. I was referred to this question - Programmatically change http response status using spring 3 restful but it did not help.
I have this Controller class with an @ExceptionHandler
method
@ExceptionHandler(CustomException.class)
@ResponseBody
public ResponseEntity<?> handleException(CustomException e) {
return new ResponseEntity<MyErrorResponse>(
new MyErrorResponse(e.getCode(), ExceptionUtility.getMessage(e.getMessage())),
ExceptionUtility.getHttpCode(e.getCode()));
}
ExceptionUtility
is a class where I have the two methods used above (getMessage
and getCode
).
public class ExceptionUtility {
public static String getMessage(String message) {
return message;
}
public static HttpStatus getHttpCode(String code) {
return HttpStatus.NOT_FOUND; //how to return status code dynamically here ?
}
}
I do not want to check in if condition and return the response code accordingly, Is there any other better approach to do this?
You need to define different Exception handler for different Exception and then use
@ResponseStatus
as below:Here the
InvalidException.class
,DuplicateDataException.class
etc are examples. You can define your custom Exceptions and throw them from controller layer. for example you can define aUserAlreadyExistsException
and return theHttpStatus.CONFLICT
error code from your exception handler.Your
@ExceptionHandler
method needs two things: (i) have anHttpServletResponse
parameter, so that you can set the response status code; and (ii) do not have the@ResponseStatus
annotation.I have used this kind of
@ExceptionHandler
to handleNestedServletException
, which is an exception that Spring sometimes creates to wrap the "actual" exception that needs to be treated (code below). Note that the@ExceptionHandler
method can have both request and response objects as parameters, if you need them.