I have a Client and Server module in my Spring project running on separate ports. The Client module makes a POST request to the Server via a RestTemplate. The Server-Module throws a custom Exception with a custom error-message. Currently, in my Project, the Server has a RestControllerAdvice Class that handles such exceptions as follows:
@RestControllerAdvice public class AppRestControllerAdvice { @ExceptionHandler(ApiException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public MessageData handle(ApiException e) { MessageData data = new MessageData(); data.setMessage(e.getMessage()); return data; } } On the Client side, the following method catches the Response from the Server.
@RestControllerAdvice public class AppRestControllerAdvice { @ExceptionHandler(ApiException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public MessageData handle(ApiException e) { MessageData data = new MessageData(); data.setMessage(e.getMessage()); return data; } @ExceptionHandler(Throwable.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public MessageData handle(Throwable e) { MessageData data = new MessageData(); data.setMessage("UNKNOWN ERROR- " + e.getMessage()); e.printStackTrace(); return data; } } Whenever the Exception is thrown on the server, here is what I receive on the Client
{ "message": "UNKNOWN ERROR- org.springframework.web.client.HttpClientErrorException: 400 Bad Request" } My question is, how do I retrieve the Custom Exception message that originated on the Server?
Also, why isn't the correct RestControllerAdvice module on the Client side picking up the error? (The INTERNAL_SERVER_ERROR method catches the error instead of the BAD_REQUEST method.)