Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-projects-issues opened this issue Apr 3, 2015 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 3, 2015

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

@spring-projects-issues
Copy link
Collaborator Author

Arjen Poutsma commented

Fixed. PR at #776

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants