0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

reactor.netty.http.client.PrematureCloseException: Connection prematurely closed DURING responseについての調査

Last updated at Posted at 2025-05-07

使用フレームワーク

Spring Framework 6 の WebClient
reactor-netty

なぜこの例外が発生するのか

Connection: close は 「レスポンスを返したらサーバーが即座に接続を切る」という意味です。
そのため、HTTPクライアント側がまだレスポンスを読み取っている途中で接続が切られた場合、httpクライアントのライブラリは例外を発生します。
reactor-netty は非同期・ノンブロッキングなので、まだレスポンス本文を全部受信していない状態で切断されるとPrematureCloseExceptionの例外をスローします。
PrematureCloseExceptionは、レスポンスの途中で接続が閉じられた(=完全にレスポンスを読み込む前に切断された)ことを示します。

解決策・対応方法

1. Connection: close を明示的に設定しない

通常、WebClient/reactor-netty は接続の管理を自動で行います。特別な理由がなければ、Connection: close を指定しない方が安全です。

2. それでも Connection: close が必要な場合

どうしても Connection: close を設定する必要がある場合は、以下のような方法で レスポンス本文が完全に読み込まれる前提で対処する必要があります:

WebClient client = WebClient.builder()
    .clientConnector(new ReactorClientHttpConnector(
        HttpClient.create()
            .doOnRequest((req, conn) -> req.requestHeaders().set("Connection", "close"))
    ))
    .build();

そのうえで、bodyToMono や bodyToFlux で 完全に読み込む処理を記述し、block() などで読み込みを完了させてください。

String response = client.get()
    .uri("/some-endpoint")
    .retrieve()
    .bodyToMono(String.class)
    .block();  // レスポンスを完全に読み込む
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?