We have upgraded from Spring Boot 3.1.6 to 3.2 and now one of our integration test cases is failing. It uses the TestRestTemplateto send a dummy request to the REST controller. This should be intercepted by a filter that rejects all requests with a content length greater than a configured value, in the test this value is 1.

Test Code:

ResponseEntity<ErrorResponse> response = restTemplate.postForEntity( "/test",
      new TestData( "test" ), ErrorResponse.class );

Prod Code: The filter impletent a OncePerRequestFilter filter and throws an exception when the following condition is met:

private boolean isRequestMaxSizeExceeded( HttpServletRequest request ) {
   return request.getContentLengthLong() > validationProperties.getMaxRequestSize().toBytes();
}

We have debugged and found that request.getContentLengthLong() returns -1 instead of 15 for our test JSON.

Is this behaviour expected? We couldn't find anything in the migration guide explaining a change here.

Comment From: snicoll

@jwedel that doesn't ring a bell. Can you please move that code in text into a sample that we can actually run? You can attach a zip to this issue or push the code to a separate GitHub repository.

Comment From: poutsma

As of Spring Framework 6.1, we no longer buffer request bodies before sending them on the wire, to reduce memory usage. As a result, for certain content types such as JSON, the contents size is no longer known upfront, and a we cannot set the Content-Length header.

If you would like to buffer request bodies like before, simply wrap the ClientHttpRequestFactory you are using in a BufferingClientHttpRequestFactory, so something like:

restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));

I will make a note of this in the migration guide.

Comment From: poutsma

I made a note here.