Closed
Description
Jerzy Krolak opened SPR-15404 and commented
The default RestTemplate
error handler throws HttpClientErrorException
or HttpServerErrorException
, which are quite general, compared to the variety of HTTP error codes and their REST semantics.
In a heavily rest-based application, introducing subclasses of HttpClientErrorException
and HttpServerErrorException
could simplify quite a lot of my code.
Sample code, actual:
try {
Book book = restTemplate.getForObject("http://example.com/books/1", Book.class);
return book;
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
// handle the 404 error
}
throw e; // can't do anything about other http errors, anyway
}
Sample code, possible:
try {
Book book = restTemplate.getForObject("http://example.com/books/1", Book.class);
return book;
} catch (HttpNotFoundErrorException e) {
// handle the 404 error
}
This change should be pretty easy to introduce, and by subclassing HttpClientErrorException
and HttpServerErrorException
should be backward compatible.
I could prepare an appropriate pull request, if you like the general idea.
Affects: 5.0.8
Issue Links:
- Improve message for
HttpStatusCodeException
with empty status text #22162 HTTP error 404 from spring web causes "404 null" message in RestTemplate - Specific exceptions for missing request headers, cookies, etc [SPR-14818] #19384 Specific exceptions for missing request headers, cookies, etc
- More error details in RestTemplate client and server exception [SPR-17130] #21667 More error details in RestTemplate client and server exception