I was wondering how it is possible to return a custom status code and message from a rest endpoint when throwing an exception. The code below allows me to throw my own, custom status code 571 when UserDuplicatedException is thrown, but I cant seem to fnd a way to give it an additional message or reason for the error. Can you help please?
@ControllerAdvice
public class ExceptionResolver {
@ExceptionHandler(UserDuplicatedException.class)
public void resolveAndWriteException(Exception exception, HttpServletResponse response) throws IOException {
int status = 571;
response.setStatus(status);
}
}
HttpServletResponsehas asendErrormethod, but that has other implications documented in its javadoc. Look into it.