I have Restful API that responds 404 errors when an item is not found, but have different messages depending on the reason why the item is not found (unknown, not available, …), it is done this way using Spring MVC :
response.sendError(HttpServletResponse.SC_NOT_FOUND, "NOT_AVAILABLE");
(this works fine with any browser, displaying 404 errors with “NOT_AVAILABLE” as message)
Now, I’d like to get that message back into my Java code using Spring RestTemplate in order to manage them.
I tried this:
try {
rest.put(apiRootUrl + "/item/{itemId}", null, itemId);
} catch (HttpClientErrorException e) {
if (NOT_FOUND == e.getStatusCode()) {
switch (MyErrorCode.valueOf(e.getStatusText())) {
case NOT_AVAILABLE:
return displayNotAvailableError();
case UNKNOWN:
return displayUnknownError();
default:
// ...
}
}
}
but in the getStatusText(), I always got the NOT FOUND label (from 404) and not my custom value.
Does someone know if it is possible to retrieve the custom message from 404 errors? And therefore how?
thanks a lot!
Edit: I run on a Tomcat server (which sends the 404 HTML page) and this does not append with a Weblogic server
OK, after digging a bit more, the solution is in fact very simple !
Tomcat was sending back to me its default 404 page, I just needed to tell it to use mine!
How to do this:
create a 404.jsp page like this:
reference it in your web.xml
And now, you can get the message in your client using
e.getStatusText()