Yes, this is a perfectly acceptable way. However, instead of using it, try-catchI would suggest implementing exception handlers for your REST controllers. This way you do not have to clutter up your REST methods.
In addition, it would be better to create a model object in the REST layer for error messages - ErrorResponsewith relevant information:
class ErrorResponse {
int statusCode;
String errorMessage;
}
And return the object from the exception handlers. BTW, you can also map your exception class directly to the response using the annotation @ResponseStatus:
@ResponseStatus(value=401, reason="message")
class Exception_Y extends RuntimeException {
}
Then you do not need to write an exception handler for this exception.
source
share