Handling gzipped requests in a Spring Boot REST application

B255 :

I have a Spring Boot REST app (1.5.6.RELEASE). I would like gzip compression incoming and outgoing. As per this documentation https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html I have set

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

But this seems to only apply to gzipping responses from my service (and this is what the doc says actually "# If response compression is enabled.").

My problem is that incoming gzipped requests are not being decompressed, resulting in JSON parsing errors.

Does anyone know how I can turn on request decompression in my Spring Boot app?

EDIT An example:

POM snippet:

<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

The gzipped call fails 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":"/"}
Brian Clozel :

The server.compression.* configuration keys are about HTTP response compression only. I'm not aware of any general solution, nor if servers support that natively.

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=457324&siteId=1