I am using spring boot with following dependency on Jdk 11

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
</parent>

this is my resttemplate config.

@Bean(name = "customRestTemplate")
    public RestTemplate getResttemplate() {
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();        
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));        
        messageConverters.add(converter);  
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(new RequestResponseLogInterceptor());
        restTemplate.setMessageConverters(messageConverters); 
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
    }

this is my interceptor

public class RequestResponseLogInterceptor implements ClientHttpRequestInterceptor {

    private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
            throws IOException {
        traceRequest(request, body);
        ClientHttpResponse response = execution.execute(request, body);
        traceResponse(response);
        return response;
    }

    private void traceRequest(HttpRequest request, byte[] body) throws IOException {
        log.info("===========================request begin================================================");
        log.debug("URI         : {}", request.getURI());
        log.debug("Method      : {}", request.getMethod());
        log.debug("Headers     : {}", request.getHeaders());
        log.debug("Request body: {}", new String(body, StandardCharsets.UTF_8));
        log.info("==========================request end================================================");
    }

    private void traceResponse(ClientHttpResponse response) throws IOException {
        StringBuilder inputStringBuilder = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody(), StandardCharsets.UTF_8));
        String line = bufferedReader.readLine();
        while (line != null) {
            inputStringBuilder.append(line);
            inputStringBuilder.append('\n');
            line = bufferedReader.readLine();
        }
        log.info("============================response begin==========================================");
        log.debug("Status code  : {}", response.getStatusCode());
        log.debug("Status text  : {}", response.getStatusText());
        log.debug("Headers      : {}", response.getHeaders());
        log.debug("Response body: {}", inputStringBuilder.toString());
        log.info("=======================response end=================================================");
    }

}

I am using resttemplate exchange after autowired my customRestTemplate

responseEntity = template.exchange(url, method, entity, Object.class);

this configuration works well, but when my client throws http 422, ClientHttpResponse response body is null.

Capture1 Capture2

Comment From: rstoyanchev

There is no reason why a specific status code give a different result. Can you please confirm if the server is sending response content?

Comment From: foxivolunteer

if I use default resttemplate.

RestTemplate template = new RestTemplate(); template.exchange()

it throws HttpClientErrorException, and I can take the responseBody with e.getResponseBodyAsString().

but if I use my custom resttemplate like I explained before, there is no body and I cannot find the exception body, because customeresttemplate is masking the exception body somehow.

this is custom resttemplate RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));

maybe this generation is masking the exception, thats why I cannot see anything in

ClientHttpResponse response = execution.execute(request, body); here

related picture is when I use default resttemplate.

RestTemplate template = new RestTemplate();

Cpt1

Comment From: rstoyanchev

Can you provide a sample to reproduce it?

Comment From: foxivolunteer

hello. i realized it is not http 422, it is 401, and this isue is discussed in https://github.com/spring-projects/spring-framework/issues/21321

here. so we can close this issue.