I would like to set the content-length response header to one of my controllers as it is needed. After referring these two questions (First, Second), I can able to understand that setting response header would be critical. But, I need this for sure. My code snippet is as follows,
@GetMapping(value = /employees, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, List<Object>>> getEmployeeDetails(@RequestParam(value = "organization_id", required = false) Integer orgId) {
Map<String, List<Object>> responseMap = //some method returns the data
ObjectMapper objMapper = new ObjectMapper();
String responseString = objMapper.writeValueAsString(responseMap);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(responseString.length()));
return ResponseEntity.ok().headers(httpHeaders).body(responseMap);
}
In the above case, Content-Length is not calculated correctly and the response json is shrinked. (i.e if the map contains 50 objects, response is shrinked somewhere inbetween)
Please help how to achieve the expected outcome. Thanks in advance.