How to set HTTP response timeout with Spring Webclient when using JDK HTTPClient

25 Views Asked by At

I am using Spring boot v3.2.3 with JDK17.

I have set up Spring Webclient with the underlying client being JDK HTTP client by following the steps on the Spring docs

The issue is that, although I can set a connection timeout, I do not see a way to set the 'response timeout' with this setup. As per the JDK documentation, typically the response timeout is set on a per HTTP Request level.

I see that Webclient allows one to access the underlying ClientHttpRequest via the 'httpRequest' function.

However, the JDK HTTPRequest itself is immutable, and the reactive/JdkclientHttpRequest class itself does not seem to allow setting a response timeout, so using the httpRequest function does not seem like an option.

I understand Mono itself has a timeout function that allows signalling a TimeoutException. However, I could not find documentation on how Webclient manages the underlying request in such an event. It seems more desirable to set the timeout on the HTTP request itself (or client, if possible), rather than using Mono timeouts.

What is the recommended approach for setting a response timeout when using Webclient + JDK HTTPClient ?

0

There are 0 best solutions below