在Spring Boot REST应用程序中处理压缩的请求 [英] Handling gzipped requests in a Spring Boot REST application

查看:92
本文介绍了在Spring Boot REST应用程序中处理压缩的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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=...

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

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.").

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

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

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

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

编辑示例:

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>

控制器代码:

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

使用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":"/"}

推荐答案

server.compression.* 配置键仅与HTTP响应压缩有关.我不知道任何通用的解决方案,也不知道服务器是否本机支持.

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.

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

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

这篇关于在Spring Boot REST应用程序中处理压缩的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆