当我使用 Spring Cloud 构建我的微服务时,如何将内容长度添加到响应标头

Posted

技术标签:

【中文标题】当我使用 Spring Cloud 构建我的微服务时,如何将内容长度添加到响应标头【英文标题】:how to add content-length to response headers when i use spring cloud to build my micro service 【发布时间】:2017-03-03 09:14:49 【问题描述】:

我使用 Spring Cloud 构建了多个微服务,并使用 Spring Cloud Netfix 的 Zuul Server 实现的 API-Gateway 将请求路由到我们的微服务,网关配置如下:

application.yml:

server:
port: 8021

ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000

zuul:
ignoredServices: "*"
add-proxy-headers: true
#prefix: /v1

routes:
m_test:
path: /api/testService/**
sensitiveHeaders: "*"
url: http://127.0.0.1:4008/testService/  

eureka:
instance:
hostname: gateway
client:
 registerWithEureka: true
 fetchRegistry: true
serviceUrl:
  defaultZone: http://127.0.0.1:8761/eureka/

CorsFilter.java:

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter 
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException 

    HttpServletResponse response = (HttpServletResponse) res;
    HttpServletRequest request = (HttpServletRequest) req;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization,Content-length");
    response.setHeader("Access-Control-Max-Age", "1800");
    Map<String, String> map = getHeadersInfo(request);

    if (request.getMethod().equalsIgnoreCase("OPTIONS")) 
        response.setStatus(HttpServletResponse.SC_OK);
     else 
        chain.doFilter(request, response);
    


public void init(FilterConfig filterConfig) 


public void destroy() 


private Map<String, String> getHeadersInfo(HttpServletRequest request) 
    Map<String, String> map = new HashMap<>();
    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) 
        String key = (String) headerNames.nextElement();
        String value = request.getHeader(key);
        map.put(key, value);
    

    return map;


GatewaySystemApplication.java:

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class GatewaySystemApplication 

public static void main(String[] args) throws Exception 
    //SpringApplication.run(GatewaySystemApplication.class, args);
    new SpringApplicationBuilder(GatewaySystemApplication.class).web(true).run(args);


浏览器响应头是这样的:

passing through gateway layer response headers

并且我的请求的响应标头缺少内容长度, 但是当我直接调用后端服务时它具有内容长度。directly response headers

【问题讨论】:

【参考方案1】:

好吧,你没有设置 Content-Length。

(FWIW,您的 Content-Type 也是伪造的)。

【讨论】:

我使用这个 'String contentType=Files.probeContentType(Paths.get(filepath))' 来获取 contentType,并且浏览器响应 content-type 变成了这样的 'Content-Type application/ zip;charset=UTF-8' ,但响应标头中仍然没有“内容长度”。 ps:谢谢你的回答

以上是关于当我使用 Spring Cloud 构建我的微服务时,如何将内容长度添加到响应标头的主要内容,如果未能解决你的问题,请参考以下文章

基于Spring Cloud的微服务构建学习-2 Spring Boot

基于Spring Cloud的微服务构建学习-3 Spring Cloud Eureka配置详解

基于Spring Cloud的微服务构建学习-3 服务治理:Spring Cloud Eureka

基于Spring Cloud的微服务构建学习-3 服务治理-Spring Cloud Eureka之高可用注册中心

Spring Cloud构建微服务架构 消息驱动的微服务(消费分区)Dalston版

0605-Zuul构建API Gateway-使用Sidecar支持异构平台的微服务