如何gzip由org.springframework.web.client.RestTemplate?
创建的HTTP请求
我正在使用Spring 4.2.6
和Spring Boot 1.3.5
(Java SE,而不是Android或Javascript在网络浏览器中)。
我正在做一些非常大的POST
请求,我希望请求体被压缩。
我提出了两个解决方案,一个更简单,没有流,一个支持流。
如果您不需要流媒体,请使用自定义ClientHttpRequestInterceptor
,一个Spring功能。
RestTemplate rt = new RestTemplate();
rt.setInterceptors(Collections.singletonList(interceptor));
interceptor
可能是:
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
request.getHeaders().add("Content-Encoding", "gzip");
byte[] gzipped = getGzip(body);
return execution.execute(request, gzipped);
}
}
getGzip
我copied
private byte[] getGzip(byte[] body) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
try {
GZIPOutputStream zipStream = new GZIPOutputStream(byteStream);
try {
zipStream.write(body);
} finally {
zipStream.close();
}
} finally {
byteStream.close();
}
byte[] compressedData = byteStream.toByteArray();
return compressedData;
}
配置拦截器后,所有请求都将被压缩。
这种方法的缺点是它不支持流式传输,因为ClientHttpRequestInterceptor
接收内容为byte[]
如果你需要流媒体创建自定义ClientHttpRequestFactory
,比如说GZipClientHttpRequestFactory
,并像这样使用它:
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
ClientHttpRequestFactory gzipRequestFactory = new GZipClientHttpRequestFactory(requestFactory);
RestTemplate rt = new RestTemplate(gzipRequestFactory);
GZipClientHttpRequestFactory
在哪里:
public class GZipClientHttpRequestFactory extends AbstractClientHttpRequestFactoryWrapper {
public GZipClientHttpRequestFactory(ClientHttpRequestFactory requestFactory) {
super(requestFactory);
}
@Override
protected ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod, ClientHttpRequestFactory requestFactory)
throws IOException {
ClientHttpRequest delegate = requestFactory.createRequest(uri, httpMethod);
return new ZippedClientHttpRequest(delegate);
}
}
而ZippedClientHttpRequest
是:
public class ZippedClientHttpRequest extends WrapperClientHttpRequest
{
private GZIPOutputStream zip;
public ZippedClientHttpRequest(ClientHttpRequest delegate) {
super(delegate);
delegate.getHeaders().add("Content-Encoding", "gzip");
// here or in getBody could add content-length to avoid chunking
// but is it available ?
// delegate.getHeaders().add("Content-Length", "39");
}
@Override
public OutputStream getBody() throws IOException {
final OutputStream body = super.getBody();
zip = new GZIPOutputStream(body);
return zip;
}
@Override
public ClientHttpResponse execute() throws IOException {
if (zip!=null) zip.close();
return super.execute();
}
}
最后WrapperClientHttpRequest
是:
public class WrapperClientHttpRequest implements ClientHttpRequest {
private final ClientHttpRequest delegate;
protected WrapperClientHttpRequest(ClientHttpRequest delegate) {
super();
if (delegate==null)
throw new IllegalArgumentException("null delegate");
this.delegate = delegate;
}
protected final ClientHttpRequest getDelegate() {
return delegate;
}
@Override
public OutputStream getBody() throws IOException {
return delegate.getBody();
}
@Override
public HttpHeaders getHeaders() {
return delegate.getHeaders();
}
@Override
public URI getURI() {
return delegate.getURI();
}
@Override
public HttpMethod getMethod() {
return delegate.getMethod();
}
@Override
public ClientHttpResponse execute() throws IOException {
return delegate.execute();
}
}
此方法使用chunked transfer encoding创建请求,如果已知大小,则可以更改设置内容长度标头。
ClientHttpRequestInterceptor
和/或自定义ClientHttpRequestFactory
方法的优点是它适用于RestTemplate的任何方法。另一种方法是传递RequestCallback只能使用execute
方法,这是因为RestTemplate的其他方法在内部创建了自己的RequestCallback来生成内容。
顺便说一下,似乎有little support to decompress gzip request on the server。也相关:Sending gzipped data in WebRequest?指向Zip Bomb问题。我想你必须为它写some code。
主要想法是创建requestCallback
,它将您想要从gzipOutputStream
发送的数据直接复制到request
流。
RequestCallback requestCallback = new RequestCallback() {
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
GZIPOutputStream gzipOutputStream;
try {
gzipOutputStream = new GZIPOutputStream(request.getBody());
} catch (IOException ignored) {
return;
}
request.getHeaders().add("Content-Type", "application/octet-stream");
request.getHeaders().add("Content-Encoding", "gzip");
try {
String data = "Test data.";
gzipOutputStream.write(data.getBytes(StandardCharsets.UTF_8));
gzipOutputStream.flush(); // Optional in this example.
gzipOutputStream.finish();
} catch (IOException ignored) {
}
}
};
现在您可以在下一个方式使用它:
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
restTemplate.setRequestFactory(requestFactory);
ResponseExtractor<String> responseExtractor = new HttpMessageConverterExtractor<>(String.class,
restTemplate.getMessageConverters());
String response = restTemplate.execute("http://localhost:8080/gzip.php", HttpMethod.POST, requestCallback,
responseExtractor);
System.out.println(response);
链接:
除了@TestoTestini的上述答案之外,如果我们利用Java 7 +的'try-with-resources'语法,因为ByteArrayOutputStream
和GZIPOutputStream
都实现了closeable(),我们可以将getGzip函数缩小为以下内容:
private byte[] getGzip(byte[] body) throws IOException {
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
try (GZIPOutputStream zipStream = new GZIPOutputStream(byteStream)) {
zipStream.write(body);
}
byte[] compressedData = byteStream.toByteArray();
return compressedData;
}
}
(我无法找到评论@TestoTestini的原始答案并保留上述代码格式的方法,因此本答案)。