Hi,
I am trying to gzip my requests (json/jackson) to the server by
using something like
But I am having troubles doing that, and I think it is a spring bug.Code:requestHeaders.setContentEncoding(Collections.singletonList(ContentCodingType.GZIP));
I.e
those lines
convert my object into a string and write it to outputMessage.getBody();Code:JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding); try { if (this.prefixJson) { jsonGenerator.writeRaw("{} && "); } this.objectMapper.writeValue(jsonGenerator, o); }
The problem I believe is in AbstractClientHttpRequest that returns a gzipoutputstream
which is never being closed (only flushed)Code:public final OutputStream getBody() throws IOException { checkExecuted(); OutputStream body = getBodyInternal(this.headers); List<ContentCodingType> contentCodingTypes = this.getHeaders().getContentEncoding(); for (ContentCodingType contentCodingType : contentCodingTypes) { if (contentCodingType.equals(ContentCodingType.GZIP)) { return new GZIPOutputStream(body); } } return body; }
in AbstractHttpMessageConverter<T>
I think addingCode:public final void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { HttpHeaders headers = outputMessage.getHeaders(); if (headers.getContentType() == null) { if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) { contentType = getDefaultContentType(t); } if (contentType != null) { headers.setContentType(contentType); } } if (headers.getContentLength() == -1) { Long contentLength = getContentLength(t, contentType); if (contentLength != null) { headers.setContentLength(contentLength); } } writeInternal(t, outputMessage); outputMessage.getBody().flush(); }
outputMessage.getBody().close()
will solve the problem.
In addition I think it's inefficient in case of gzip to create a new object in every call to getBody.
Can you confirm, it's a bug, or I am doing something incorrectly.


Reply With Quote