Java 使用SpringRESTTemplate获取序列化请求的大小

Java 使用SpringRESTTemplate获取序列化请求的大小,java,spring,rest,spring-boot,resttemplate,Java,Spring,Rest,Spring Boot,Resttemplate,我正在使用Spring和RestTemplate向REST服务发送请求。有没有办法获得实际请求的(字节)大小?最佳情况下,它包括HTTP请求的大小,包括GET和POST请求的序列化myObject对象的大小 template.postForObject(restUrl, myObject, MyObject.class); template.getForObject(restUrl, MyObject.class); 基本上我想知道实际传输了多少数据 谢谢 [编辑]: 为了完成回答,以下是如

我正在使用Spring和RestTemplate向REST服务发送请求。有没有办法获得实际请求的(字节)大小?最佳情况下,它包括HTTP请求的大小,包括GET和POST请求的序列化
myObject
对象的大小

template.postForObject(restUrl, myObject, MyObject.class);
template.getForObject(restUrl, MyObject.class);
基本上我想知道实际传输了多少数据

谢谢


[编辑]:

为了完成回答,以下是如何将拦截器添加到RestTemplate中。我还编辑了LengthInterceptor以显示请求的内容长度,而不是响应

final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add( new LengthInterceptor() );
template.setInterceptors( interceptors );
final List interceptors=new ArrayList();
add(newlengthinterceptor());
模板.设置拦截器(拦截器);

您可以使用拦截器来截取请求/响应,类似于Javaservlet中的过滤器。您必须阅读响应并使用
getHeaders().getContentLength()
获得正文长度:

public class LengthInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept( HttpRequest request, byte[] body, ClientHttpRequestExecution execution ) throws IOException {

        ClientHttpResponse response = execution.execute( request, body );
        long length = response.getHeaders().getContentLength();
        // do something with length
        return response;
    }
}

您是指http主体的大小还是原始tcp包的大小?以及如何处理分块请求?