Spring-Boot disable transfer-coding from response header

岁酱吖の 提交于 2019-12-24 00:46:17

问题


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 ?


回答1:


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!



来源:https://stackoverflow.com/questions/50110138/spring-boot-disable-transfer-coding-from-response-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!