Skip to content

AsyncRestTemplate should trigger no-output HTTP requests immediately as well [SPR-14093] #18665

Closed

Description

@spring-projects-issues

Evgeny Vishnyakov opened SPR-14093 and commented

The class SimpleBufferingAsyncClientHttpRequest contains following code:

// JDK <1.8 doesn't support getOutputStream with HTTP DELETE
if (HttpMethod.DELETE == getMethod() && bufferedOutput.length == 0) {
	connection.setDoOutput(false);
}

The comment is true, but it doesn't check version of JVM. So without such checking the method HttpMethod.DELETE can't be invoked asynchronously.


Affects: 4.2.5

Reference URL: http://stackoverflow.com/questions/33103096/when-does-the-asyncresttemplate-send-request

Issue Links:

Activity

  1. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    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?

  2. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Evgeny Vishnyakov commented

    I'm running this code with JDK 1.8 and no request is sent on a server at all.

  3. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Evgeny Vishnyakov commented

    So

    connection.setDoOutput(false);
    

    should not be done for JDK 1.8.

  4. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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?

  5. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    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...

  6. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Evgeny Vishnyakov commented

    As I see if I use synchronize version of RestTemplate the request is sent. So the problem remains only for AsyncRestTemplate.

  7. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Evgeny Vishnyakov commented

    I found the same problem on stackoverflow and the request is sent to uri only after result.get() is called.

  8. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    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.

  9. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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...

  10. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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).

  11. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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

  12. spring-projects-issues commented on Mar 29, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Evgeny Vishnyakov commented

    Thank you for fix :)

  13. spring-projects-issues commented on Mar 30, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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 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.

  14. spring-projects-issues commented on Mar 30, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Hmm, did we ever expect a RestTemplate.execute method to throw a FileNotFoundException??

    In any case, not sure what causes a change of behavior here... A getResponseCode call seems to just initialize the response stream.

  15. spring-projects-issues commented on Mar 31, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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:

    Exception exc = null;
    try {
        getInputStream();
    } catch (Exception e) {
        exc = e;
    }

    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.

  16. added
    type: enhancement
    in: web
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions