Handling gzipped requests in a Spring Boot REST application

I have a REST (Spring) download application (1.5.6.RELEASE). I would like gzip compression to be inbound and outbound. According to this documentation https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html I installed

server.compression.enabled=true server.compression.mime-types=... 

But this, apparently, only applies to the gzipping responses of my service (and this is what the actual document says "#If response compression is enabled.").

My problem is that incoming gzipped requests are not unpacked, which leads to JSON parsing errors.

Does anyone know how I can enable request decompression in my Spring boot application?

EDIT Example:

POM mark:

 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> 

Controller Code:

 @RestController public class Controller { @RequestMapping(value = "/", method = RequestMethod.POST, consumes = "application/json") public String post(@RequestBody Map<String, String> request) { return request.get("key"); } } 

Test using curl:

 $ echo '{ "key":"hello" }' > body $ curl -X POST -H "Content-Type: application/json" --data-binary @body http://localhost:8080 # prints 'hello' $ echo '{ "key":"hello" }' | gzip > body.gz $ curl -X POST -H "Content-Type: application/json" -H "Content-Encoding: gzip" --data-binary @body.gz http://localhost:8080 # fails 

Gzipped failed with message:

 {"timestamp":1505843443456,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"JSON parse error: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\\r, \\n, \\t) is allowed between tokens; nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\\r, \\n, \\t) is allowed between tokens\n at [Source: java.io.PushbackInputStream@50ebec25 ; line: 1, column: 2]","path":"/"} 
+5
source share
1 answer

The server.compression.* Configuration keys apply only to the compression of the HTTP response. I do not know of any general solution, and if the servers support this natively.

You can support this by using the Servlet filter, which does just that, but Spring Boot does not offer this feature.

0
source

Source: https://habr.com/ru/post/1271938/


All Articles