Java 如何在 Spring MVC REST 中为 JSON 设置内容长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24156490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:32:01  来源:igfitidea点击:

How to set content-length in Spring MVC REST for JSON?

javajsonspringspring-mvccontent-length

提问by ruslanys

I have some code:

我有一些代码:

@RequestMapping(value = "/products/get", method = RequestMethod.GET)
public @ResponseBody List<Product> getProducts(@RequestParam(required = true, value = "category_id") Long categoryId) {
    // some code here
    return new ArrayList<>();
}

How could I configure Spring MVC (or MappingHymanson2HttpMessageConverter.class) to set right header Content-Length by default? Because now my response header content-lengthequal to -1.

默认情况下,如何配置 Spring MVC(或 MappingHymanson2HttpMessageConverter.class)以设置正确的标头 Content-Length?因为现在我的响应头content-length等于 -1。

回答by Woody Sun

You can add ShallowEtagHeaderFilter to filter chain. The following snippet works for me.

您可以添加 ShallowEtagHeaderFilter 来过滤链。以下代码段对我有用。

import java.util.Arrays;

import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;

@Configuration
public class FilterConfig {

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

}

The response body will looks like the below:

响应正文如下所示:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Application-Context: application:sxp:8090
ETag: "05e7d49208ba5db71c04d5c926f91f382"
Content-Type: application/json;charset=UTF-8
Content-Length: 232
Date: Wed, 16 Dec 2015 06:53:09 GMT

回答by Dmitry Buzoverya

The following filter in chain sets content-length:

链中的以下过滤器设置内容长度:

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.util.ContentCachingResponseWrapper;

public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,     FilterChain chain) throws IOException, ServletException {

        ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper((HttpServletResponse) response);

        chain.doFilter(request, responseWrapper);

        responseWrapper.copyBodyToResponse();

    }

    @Override
    public void destroy() {
    }

}

The main idea that all content is cached in ContentCachingResponseWrapper and finally content-length is set when you call copyBodyToResponse().

主要思想是所有内容都缓存在 ContentCachingResponseWrapper 中,最后在调用 copyBodyToResponse() 时设置 content-length。