Skip to content

RestClient readWithMessageConverters method hiding I/O Exceptions with incorrect error message. #37078

Open

Description

@Nicolas-Miranda

The org.springframework.web.client.DefaultRestClient#readWithMessageConverters method from spring-web:7.0.8 is hiding I/O Exceptions (SocketTimeouts from connectTimeout/readTimeout) as a RestClientException with an unrelated content-type incompatibility message:

throw new RestClientException("Error while extracting response for type [" + ResolvableType.forType(bodyType) + "] and content type [" + contentType + "]", cause);

For example a SocketTimeout exception triggered by a readTimeout set in a client configured with Accept/Content-Type headers for "application/json", throws a RestClientException with the message:

"Error while extracting response for type [MyServiceResponse] and content type [application/octet-stream]"

This message is misleading, causing confusion in the actual underlying issue. Ideally if the issue is I/O related, the readWithMessageConverters method should throw a ResourceAccessException with the proper message.

Activity

  1. added
    in: webIssues in web modules (web, webmvc, webflux, websocket)
    on Jul 21, 2026
  2. fudianchn commented on Jul 26, 2026

    @fudianchn

    定位了根因并有一个向后兼容的修复方案(暂未提 PR,因本地 Gradle 构建受网络阻断无法验证;先贴方案供参考)。

    根因:DefaultRestClient.readWithMessageConverters 的 catch 块把 UncheckedIOException | IOException | HttpMessageNotReadableException 统一包成 RestClientException("Error while extracting response for type [...] and content type [...]", cause)——即使 cause 是 I/O 异常(如 SocketTimeout),消息却指向 content type,产生误导。

    修复:cause 为 IOException 时改抛 ResourceAccessException(I/O 专用消息)。这是向后兼容的,因为 ResourceAccessException extends RestClientException(子类),捕获 RestClientException 的调用方仍能捕获。而且该类里已有同样用 ResourceAccessException 表达 I/O 错误的先例(createResourceAccessException)。

    catch (UncheckedIOException | IOException | HttpMessageNotReadableException exc) {
        Throwable cause = (exc instanceof UncheckedIOException u) ? u.getCause() : exc;
        if (cause instanceof IOException ioException) {
            throw new ResourceAccessException(
                    "I/O error while extracting response for type [" + ResolvableType.forType(bodyType) + "]",
                    ioException);
        }
        throw new RestClientException("Error while extracting response for type [" +
                ResolvableType.forType(bodyType) + "] and content type [" + contentType + "]", cause);
    }

    测试思路:mock ClientHttpResponse.getBody() 抛 IOException,retrieve().body(String.class) 应抛 ResourceAccessException(而非带 content-type 消息的 RestClientException)。

  3. akg268 commented on Aug 9, 2026

    @akg268

    Hi Spring team, I would like to work on this issue if it is open for contribution. Could you please assign it to me?

    I saw the proposed direction above around preserving the I/O failure as a ResourceAccessException from DefaultRestClient.readWithMessageConverters. Before I start, I will wait for confirmation that this is the desired fix and add focused coverage for the I/O exception case.

  4. akg268 commented on Aug 23, 2026

    @akg268

    Gentle ping on this. I am still interested in working on a focused fix for preserving I/O failures as ResourceAccessException from DefaultRestClient.readWithMessageConverters, with regression coverage. Please let me know if that direction is acceptable before I start.

  5. bclozel commented on Aug 23, 2026

    @bclozel
    Member

    @akg268 this is not yet triaged and we already received two PRs for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: waiting-for-triageAn issue we've not yet triaged or decided on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions