在Spring Boot REST应用程序中处理gzip压缩请求

B25*_*255 8 java rest spring spring-boot

我有一个Spring Boot REST应用程序(1.5.6.RELEASE).我想gzip压缩传入和传出.根据这个文档https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html我已经设置

server.compression.enabled=true
server.compression.mime-types=...
Run Code Online (Sandbox Code Playgroud)

但这似乎只适用于从我的服务gzipping响应(这就是文档实际上说的"#如果启用了响应压缩.").

我的问题是传入的gzip压缩请求没有被解压缩,导致JSON解析错误.

有谁知道如何在我的Spring Boot应用程序中打开请求解压缩?

编辑一个例子:

POM片段:

<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>
Run Code Online (Sandbox Code Playgroud)

控制器代码:

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

使用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
Run Code Online (Sandbox Code Playgroud)

gzipped调用失败,并显示以下消息:

{"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":"/"}
Run Code Online (Sandbox Code Playgroud)

Bri*_*zel 5

server.compression.*配置键是大约只有HTTP响应的压缩。我不知道任何常规解决方案,也不知道服务器是否本身支持该解决方案。

您可以使用仅执行此操作的Servlet过滤器来支持该功能,但是Spring Boot不提供该功能。