This interface is shared by the server and the client (client is openfeign 10.10--httpclient in springboot,server is springboot2.3):
@RequestMapping(value = "/user/get", method = {RequestMethod.GET})
public User userGetByDesc(@SpringQueryMap User user);
The input parameter (@SpringQueryMap User user) is very big ,and the size is dynamic, so I compress the request in openfeign:
feign.compression.request.enabled: true
But it doesn't work.
I read these codes in FeignContentGzipEncodingInterceptor.requiresCompression():
return matchesMimeType(headers.get(HttpEncoding.CONTENT_TYPE))
&& contentLengthExceedThreshold(headers.get(HttpEncoding.CONTENT_LENGTH));
I thought these two headers were added automatically. But the request has not any header when I debug the code.
So:
My 1st question is : In the case ,how to add Content-Type & Content-Length in request header that is better?
My 2nd question is : If the request header has Content-Encoding: gzip,deflate, which class does the compression work?
Thanks!
plus:
I change to POST instead:
@RequestMapping(value = "/user/get", method = {RequestMethod.POST}, headers = {"Content-Type="+MediaType.APPLICATION_JSON_VALUE})
public User userGet(@RequestBody User user);
There are Content-Type & Content-Length in header, then feign adds: Content-Encoding gzip,deflate.
But through wireshark observation, the request content is not compressed.
How to let it work?
Thanks!
Comment From: OlgaMaciaszek
Hello, @childewuque, thank for submitting the question. Please learn how to properly format code and logs.
The Content-Length header is added by OpenFeign when the request contains a body, which is in keeping with the spec for this header. It refers to entity-body. Similarly, you can read there that the Content-Encoding header also refers to the entity-body. Unlike the body, the query map should not be huge and require compression. If it does, there are probably architecture changes required to improve the API. Moreover, the encoding headers added on request side do not cause anything to happen automatically - they indicate to the server that the client expects an encoded response, but it's the responsibility of the server to actually implement and handle the encoding when such headers are present.
Comment From: childewuque
Hello, @childewuque, thank for submitting the question. Please learn how to properly format code and logs. The
Content-Lengthheader is added by OpenFeign when the request contains a body, which is in keeping with the spec for this header. It refers to entity-body. Similarly, you can read there that theContent-Encodingheader also refers to theentity-body. Unlike the body, the query map should not be huge and require compression. If it does, there are probably architecture changes required to improve the API. Moreover, the encoding headers added on request side do not cause anything to happen automatically - they indicate to the server that the client expects an encoded response, but it's the responsibility of the server to actually implement and handle the encoding when such headers are present.
Thank you very much. But you said "Moreover, the encoding headers added on request side do not cause anything to happen automatically" By the openfeign doc https://docs.spring.io/spring-cloud-openfeign/docs/2.2.11.BUILD-SNAPSHOT/reference/html/#feign-requestresponse-compression I originally thought that feign would compress the request body. But no. then what does the following configuration do? feign.compression.request.enabled=true
Comment From: childewuque
@OlgaMaciaszek Please help explain this configuration item: feign.compression.request.enabled=true Thank you!
Comment From: OlgaMaciaszek
When the request matches the mime type set in feign.compression.request.mime-types and the size set in feign.compression.request.min-request-size, feign.compression.request.enabled=true results in the following headers being added to the request: Content-Encoding=gzip,deflate. You can see the code here. The header is added to the request and gives information for server processing as described here.