spring boot 返回 Transfer-Encoding: chunked头而不是Content-Length

3月 15, 2023 |

spring boot 返回 Transfer-Encoding: chunked头而不是Content-Length

@GetMapping("/sayHello")
public R<String> sayHello() {
	return R.ok("qwerty");
}

curl -v -s "http://localhost:8082/sayHello"

HTTP/1.1 200
Content-Type: application/json
Transfer-Encoding: chunked
Date: Thu, 16 Mar 2023 03:28:06 GMT
{"code":200,"msg":null,"data":"qwerty"}

解决办法

参考连接中spring官方的回答,人家认为返回Transfer-Encoding: chunked没有什么不妥的。
可以通过添加ShallowEtagHeaderFilter过滤器强制添加Content-Length头

@Configuration
public class Config {
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterBean = new FilterRegistrationBean();
        filterBean.setFilter(new ShallowEtagHeaderFilter());
        filterBean.setUrlPatterns(Arrays.asList("*"));
        return filterBean;
    }
}

参考文档

https://github.com/spring-projects/spring-framework/issues/21263#issuecomment-453470528

Posted in: spring practise

Comments are closed.