Spring-Boot禁用响应头中的传输编码 [英] Spring-Boot disable transfer-coding from response header

查看:697
本文介绍了Spring-Boot禁用响应头中的传输编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题语句-Spring-Boot(2.0.1.RELEASE和嵌入式Tomcat Server)中的一个简单的RESTful服务返回如下响应,

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

经过查找后,我发现这是由于标头Transfer-Encoding: chunked引起的.尝试在application.properties

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

但是,没有用.有什么办法可以禁用相同的功能吗?
我应该编写显式代码以将参数设置为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 ?

推荐答案

这可以通过显式添加HttpHeaders.CONTENT_LENGTH标头来实现,如下所示:

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

一个例子:

@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)
{ 
    ... 
}

您可能要在SO上通过答案更多细节.

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

希望这会有所帮助!

这篇关于Spring-Boot禁用响应头中的传输编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆