Skip to content

Exception hierarchy under HttpClientException and HttpServerException for the RestTemplate [SPR-15404] #19967

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Referenced from: commits 7f0e348, 2216964

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions