You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exception hierarchy under HttpClientException and HttpServerException for the RestTemplate [SPR-15404] #19967
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.
The implementation creates a package cycle where .client depends on .client.response and vice-versa. A sub-class of DefaultResponseErrorHandler together with the exceptions could break the cycle. Or the exceptions would move up but they're too many.
Another idea is to have them nested in either HttpClientErrorException or HttpServerErrorException with a slightly abbreviated name so it becomes:
} catch (HttpClientErrorException.BadRequestex) {
// handle the 400
}
or:
} catch (HttpClientErrorException.BadRequest400ex) {
// handle the 400
}
Looks good to me as well. I like Rossen's idea of nesting the exceptions in their "parent", as I don't think we should introduce so many new public types (and files!) this late in RestTemplate's lifecycle.
Activity
spring-projects-issues commentedon Jul 2, 2018
Jerzy Krolak commented
I know it's been over a year, but I still see a point in implementing this feature.
I've written a piece of code to illustrate my idea - maybe you could have a look at that: https://github.com/jerzykrlk/spring-framework/pull/1/files
I can polish it up later.
spring-projects-issues commentedon Jul 2, 2018
Rossen Stoyanchev commented
On a first glance it seems okay to me. Arjen Poutsma or Juergen Hoeller?
The implementation creates a package cycle where
.clientdepends on.client.responseand vice-versa. A sub-class of DefaultResponseErrorHandler together with the exceptions could break the cycle. Or the exceptions would move up but they're too many.Another idea is to have them nested in either HttpClientErrorException or HttpServerErrorException with a slightly abbreviated name so it becomes:
or:
spring-projects-issues commentedon Jul 3, 2018
Arjen Poutsma commented
Looks good to me as well. I like Rossen's idea of nesting the exceptions in their "parent", as I don't think we should introduce so many new public types (and files!) this late in RestTemplate's lifecycle.
spring-projects-issues commentedon Jul 3, 2018
Jerzy Krolak commented
Thanks for your feedback - I'll update the code tomorrow. Meanwhile, a few more questions:
HttpClientErrorException?and one more general:
... I could see this:
or even nicer...
spring-projects-issues commentedon Jul 4, 2018
Jerzy Krolak commented
Hi. I've updated the code to static inner class exceptions - see https://github.com/jerzykrlk/spring-framework/pull/1/files - can you have a look now?
spring-projects-issues commentedon Aug 6, 2018
Jerzy Krolak commented
Thank you!