spring-projects / spring-projects/spring-framework

Functional style error fallback support in RestClient.retrieve()

Open
#35,735 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: web status: waiting-for-triage
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

The RestClient is great. However, its error handling design is currently limited to side effect style callbacks through .onStatus(...), where the typical behavior is to throw exceptions. 😬

In many cases, especially when writing resilient or fault-tolerant clients, developers would prefer to use functional style fallbacks, returning a default or cached value when the response status indicates an error, instead of throwing and catching exceptions manually. It helps us to write clean code.

Current Behavior

Book book;
try {
    book = restClient.get()
        .uri("/books/{id}", id)
        .retrieve()
        .body(Book.class);
} catch (RestClientException ex) {
    book = defaultBook;
}

Proposed Enhancement
Introduce a functional-style error fallback mechanism (similar to WebClient’s reactive operators).

Book book = restClient.get()
                 .uri("/books/{id}", id)
                 .retrieve()
                 .onStatus(HttpStatusCode::is5xxServerError, (req, res) -> defaultBook)
                 .body(Book.class);

Motivation

  • Keeps the API consistent with the functional style of WebClient.
  • Reduces boilerplate and improves readability for simple fallback cases.
  • Encourages exception-free control flow for predictable errors like 404 or 500.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the RestClient.retrieve() and onStatus(...) entry points, then compare the proposed behavior with WebClient’s reactive operators. The issue does not name files or tests, so first map the existing status-handling flow. Done means a functional fallback can be selected for matching response statuses and returns the fallback value instead of requiring exception handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.