5

Problem statement - a simple RESTful service in Spring-Boot (2.0.1.RELEASE, and embedded Tomcat Server) returns response like,

HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 01 May 2018 00:33:04 GMT

7d
{the-json-response-anticipated}
0

After a search-and-find, I found that this is caused due to the header Transfer-Encoding: chunked. Tried setting the following in application.properties

spring.http.encoding.force=false
spring.http.encoding.enabled=false

But, to no use. Any means to disable the same ?
Should I write explicit code to form a header with the parameter set asfalse and set it to the header of the response ?

soufrk
  • 825
  • 1
  • 10
  • 24
  • 1
    Why do you want to disable a `chunked` response? For large responses, `chunked` encoding is the only way to avoid *huge* buffers on the server side. – Christopher Schultz May 06 '18 at 15:26
  • Let's say the response is short and predefined, and I want to observe if time can be cut by an upfront non-chunked response. As whoever is consuming the service has still not upgraded to modern standards and wants a reason. – soufrk May 09 '18 at 08:46

1 Answers1

2

This can be achieved by explicitly adding the HttpHeaders.CONTENT_LENGTH header like the below:

An example:

@RequestMapping(value = "/contacts", method = RequestMethod.POST)
public Map<String, ContactInfo> addContactInfo(
                            @RequestBody Map<String, ContactInfo> ContactInfoDto,    
                            @RequestHeader(value = HttpHeaders.CONTENT_LENGTH, required = true) Long contentLength)
{ 
    ... 
}

You may want to go through this answer on SO for more details.

Hope this helps!

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
  • I'm not sure about the mechanics, here (i.e. annotations), but yes, the proper way to avoid chunked encoding is to specify a `Content-Length` response header. – Christopher Schultz May 09 '18 at 16:30
  • This code will require that the caller set the content-length header to use this endpoint. I believe what is being asked for here is how to change this on the response. The accepted answer in that link looks more appropriate. – Robert Zahm Dec 07 '18 at 20:16