You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AsyncRestTemplate should trigger no-output HTTP requests immediately as well [SPR-14093] #18665
What behavior are you seeing specifically? The above code only really suppress the doOutput flag if no output is to be written anyway... Does this affect HttpURLConnection's internal behavior beyond simply not writing to the output stream?
setDoOutput remains false for other HTTP methods without a body as well though, even on JDK 8, so it is surprising that it somehow causes the request not being sent at all...
Hmm, so the problem is rather that for any request with doOutput==false, we only actually send the request once Future.get() is being called... which of course isn't quite intended. This rather seems to be a side effect from URLConnection's design: That strange old API doesn't necessarily trigger the request on connect() but does so on an getOutputStream() call (even if nothing is being written) or, for no-output requests, on getInputStream(). And the latter is only being called when Future.get() comes in... I guess we need to enforce its call earlier, in particular for no-output scenarios: This would fix the issue both for GET and DELETE requests with empty body.
Assuming it is indeed about late triggering (on Future.get()) of no-output requests, which is inconsistent with the early triggering of do-output requests, let's refine this for 4.3 (RC1 coming up next week). While this wasn't really intended in the first place, the fix is a behavioral change and should arguably only happen in the 4.3 line.
Please give it a try once available in the upcoming 4.3.0.BUILD-SNAPSHOT...
There's one small side-effect with this change that affected a Spring Boot test. Before the change, calling request.execute() would throw a FileNotFoundException for a 404 status. I'm not entirely sure why that no longer happens after the change, I guess it's because calling connection.getResponseCode() catches and swallows that exception. The fix was easy enough, just thought I should mention it.
I'm not sure why we ended up catching it, my best guess is that we weren't expecting initially but found it happened and then started relying on it. The HttpURLConnection.getResponseCode() method does this:
So my best guess it that even though that throws, the headers are still read and a 404 status is returned. Perhaps a second call getInputStream implementation then does something different. I'm not to sure.
I think the new behavior is correct, I just wanted to log a note in case someone else hits it.
Activity
spring-projects-issues commentedon Mar 29, 2016
Juergen Hoeller commented
What behavior are you seeing specifically? The above code only really suppress the
doOutputflag if no output is to be written anyway... Does this affectHttpURLConnection's internal behavior beyond simply not writing to the output stream?spring-projects-issues commentedon Mar 29, 2016
Evgeny Vishnyakov commented
I'm running this code with JDK 1.8 and no request is sent on a server at all.
spring-projects-issues commentedon Mar 29, 2016
Evgeny Vishnyakov commented
So
should not be done for JDK 1.8.
spring-projects-issues commentedon Mar 29, 2016
Juergen Hoeller commented
Ouch, that's pretty bad... So I suppose it doesn't work for you in the synchronous case either, since we apply the same code there?
spring-projects-issues commentedon Mar 29, 2016
Juergen Hoeller commented
setDoOutputremainsfalsefor other HTTP methods without a body as well though, even on JDK 8, so it is surprising that it somehow causes the request not being sent at all...spring-projects-issues commentedon Mar 29, 2016
Evgeny Vishnyakov commented
As I see if I use synchronize version of RestTemplate the request is sent. So the problem remains only for AsyncRestTemplate.
spring-projects-issues commentedon Mar 29, 2016
Evgeny Vishnyakov commented
I found the same problem on stackoverflow and the request is sent to uri only after result.get() is called.
spring-projects-issues commentedon Mar 29, 2016
Juergen Hoeller commented
Hmm, so the problem is rather that for any request with
doOutput==false, we only actually send the request onceFuture.get()is being called... which of course isn't quite intended. This rather seems to be a side effect fromURLConnection's design: That strange old API doesn't necessarily trigger the request onconnect()but does so on angetOutputStream()call (even if nothing is being written) or, for no-output requests, ongetInputStream(). And the latter is only being called whenFuture.get()comes in... I guess we need to enforce its call earlier, in particular for no-output scenarios: This would fix the issue both for GET and DELETE requests with empty body.spring-projects-issues commentedon Mar 29, 2016
Juergen Hoeller commented
Assuming it is indeed about late triggering (on
Future.get()) of no-output requests, which is inconsistent with the early triggering of do-output requests, let's refine this for 4.3 (RC1 coming up next week). While this wasn't really intended in the first place, the fix is a behavioral change and should arguably only happen in the 4.3 line.Please give it a try once available in the upcoming
4.3.0.BUILD-SNAPSHOT...spring-projects-issues commentedon Mar 29, 2016
Evgeny Vishnyakov commented
I have tried 4.3.0.BUILD-SNAPSHOT and I see the same issue. The only solution is to invoke Future.get() or connection.setDoOutput(true).
spring-projects-issues commentedon Mar 29, 2016
Juergen Hoeller commented
Oh, sorry for not having been clear enough: I meant the upcoming snapshot... You can track its CI job here: https://build.spring.io/browse/SPR-PUB-3389
spring-projects-issues commentedon Mar 29, 2016
Evgeny Vishnyakov commented
Thank you for fix :)
spring-projects-issues commentedon Mar 30, 2016
Phil Webb commented
There's one small side-effect with this change that affected a Spring Boot test. Before the change, calling
request.execute()would throw aFileNotFoundExceptionfor a 404 status. I'm not entirely sure why that no longer happens after the change, I guess it's because callingconnection.getResponseCode()catches and swallows that exception. The fix was easy enough, just thought I should mention it.spring-projects-issues commentedon Mar 30, 2016
Juergen Hoeller commented
Hmm, did we ever expect a
RestTemplate.executemethod to throw aFileNotFoundException??In any case, not sure what causes a change of behavior here... A
getResponseCodecall seems to just initialize the response stream.spring-projects-issues commentedon Mar 31, 2016
Phil Webb commented
I'm not sure why we ended up catching it, my best guess is that we weren't expecting initially but found it happened and then started relying on it. The
HttpURLConnection.getResponseCode()method does this:So my best guess it that even though that throws, the headers are still read and a 404 status is returned. Perhaps a second call
getInputStreamimplementation then does something different. I'm not to sure.I think the new behavior is correct, I just wanted to log a note in case someone else hits it.