Skip to content

HttpClientErrorException missing response body if callback is registered [SPR-12887] #17485

@spring-projects-issues

Description

@spring-projects-issues

Fedor Bartosh opened SPR-12887 and commented

AsyncRestTemplate.execute returns ListenableFuture that allows to register callbacks. Let's say we have following example:


    public void test() throws InterruptedException, ExecutionException {
        AsyncRestTemplate restTemplate = new AsyncRestTemplate();
        String url = "http://graph.facebook.com/some-non-existing-resource";
        ListenableFuture<ResponseEntity<Object>> responseEntity = restTemplate.exchange(url, HttpMethod.GET, HttpEntity.EMPTY, Object.class);
        final String[] listenerExceptionBody = new String[1];
        String getExceptionBody = null;
        try {
            responseEntity.addCallback(new ListenableFutureCallback<ResponseEntity<Object>>() {
                @Override
                public void onFailure(Throwable throwable) {
                    if (throwable instanceof HttpClientErrorException) {
                        HttpClientErrorException exception = (HttpClientErrorException) throwable;
                        listenerExceptionBody[0] = exception.getResponseBodyAsString();
                    }
                }

                @Override
                public void onSuccess(ResponseEntity<Object> objectResponseEntity) {
                    System.out.println("onSuccess");
                }
            });

            Thread.sleep(100);
            responseEntity.get();
        } catch (HttpClientErrorException e) {
            getExceptionBody = e.getResponseBodyAsString();
        }
        Assert.assertEquals(listenerExceptionBody[0], getExceptionBody);
    }

If 4xx error occurs during REST call, two HttpClientErrorException will be created, one of them containing response body and another one missing.

Sample maven project is attached.


Affects: 4.1.6

Attachments:

Issue Links:

Referenced from: commits b119a9c, de0f3ae, 3402c13, b18053f

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions