Why is HttpStatusCodeException.getResponseBodyAsString () empty when called after restTemplate.exchange with httpMethod.POST

I just ran into this problem and cannot understand why this is happening.

code snippet client:

try {
    ResponseEntity<Credentials> response = this.restTemplate.exchange(uri, HttpMethod.POST, httpEntity,
                    Credentials.class);
    this.userCredentials = response.getBody();
    return this.userCredentials;
} catch (HttpStatusCodeException hsce) {
    logger.error("loginUser failed with code " + hsce.getMessage());
    ObjectMapper mapper = new ObjectMapper();
    TransmissionFailureException tfe = new TransmissionFailureException();
    try {
    if (hsce.getStatusCode() == HttpStatus.UNAUTHORIZED) {
        logger.warn(" HIER " + hsce.getMessage());
        throw (CredentialsIncorrectException) mapper.readValue(hsce.getResponseBodyAsString(), CredentialsIncorrectException.class);
...

When I install HttpMethod in GET (of course, on the server side), hsce.getResponseBodyAsString () returns a JSON representation of my error as String, as expected, and Parsing / Mapping works as expected.

It is strange that the same request made using SoapUI has an exception in the body, as expected in POST and GET.

Am I misunderstood something or is this some kind of mistake?

Any help would be appreciated.

Yours faithfully

Edit: Json Body displayed by Soap UI using GET:

{
"cause": null,
"stackTrace":    [
...
],
"localizedMessage": "No user found with given username...",
"message": "No user found with given username...",
"suppressed": []
}

Json Body displayed by Soap user interface using POST:

{
"cause": null,
"stackTrace":    [
        ...
],
"localizedMessage": "No user found with given username...",
"message": "No user found with given username...",
"suppressed": []
}

StackTrace , ...

+4
1

, .

": RestTemplate JDK HTTP-. HTTP, Apache HttpComponents, Netty OkHttp HttpAccessor.setRequestFactory(org.springframework.http. client.ClientHttpRequestFactory).

doc :

, java.net HTTP- , (, 401). , HttpComponentsClientHttpRequestFactory.

java.net impl , 401 ().

, factory RestTemplate.

    RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());

    template.setRequestFactory(new HttpComponentsClientHttpRequestFactory());

http- apache. .

+5

Source: https://habr.com/ru/post/1612381/


All Articles