Spring REST Controller 不处理 gzip 压缩输入

标签 spring spring-boot jackson spring-restcontroller

我已经根据文档在 application.properties 中设置以下属性,将 Spring Boot 应用程序设置为处理 HTTP 服务器压缩:

server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css

尽管如此,在接收到 gzip 压缩输入后,Spring REST Controller 似乎无法解码输入。我收到一个 HttpMessageNotReadableException。以下是日志中的相关行:

2020-08-17 14:44:41,201 DEBUG [http-nio-9972-exec-4] org.springframework.web.filter.CommonsRequestLoggingFilter: BEFORE REQUEST: POST /api/v1/observerConfiguration/observer, client=127.0.0.1, headers=[accept-encoding:"gzip", "deflate", content-encoding:"gzip", "deflate", accept:"*/*", user-agent:"Java/11.0.8", host:"localhost:9972", connection:"keep-alive", transfer-encoding:"chunked", Content-Type:"application/json;charset=UTF-8"]]
2020-08-17 14:44:41,512 WARN  [http-nio-9972-exec-4] org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver: Resolved [org.springframework.http.converter.HttpMessageNotReadableException: 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
 at [Source: (PushbackInputStream); line: 1, column: 2]]
2020-08-17 14:44:41,513 DEBUG [http-nio-9972-exec-4] org.springframework.web.filter.CommonsRequestLoggingFilter: AFTER REQUEST: POST /api/v1/observerConfiguration/observer, client=127.0.0.1, headers=[accept-encoding:"gzip", "deflate", content-encoding:"gzip", "deflate", accept:"*/*", user-agent:"Java/11.0.8", host:"localhost:9972", connection:"keep-alive", transfer-encoding:"chunked", Content-Type:"application/json;charset=UTF-8"]]

是否需要一些额外的配置才能使 Spring Boot 处理压缩的 JSON 负载?

Spring Boot 版本为 v2.3.2.RELEASE

最佳答案

我相信server.compression.enabled=true是关于响应,而不是请求。 对于请求,您可能需要编写自己的Filter或使用任何现有的。 在互联网上查找,我可以找到this一些可能有帮助的例子。

关于Spring REST Controller 不处理 gzip 压缩输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63451316/

相关文章:

java - 如何使用 Spring dbunit 模拟数据库 View ?

rest - 使用 Scheduler 和 Rest 调用 Spring Boot 集成测试应用程序

java - jackson - 如何将空值反序列化为 null

java - 如何使用 Java 从 Yaml 文件中读取列表映射

java - MongoDB 和用户特定文档——Spring Security 后端

java - 数据库更新后运行的 PUT 和 PATCH 的 Spring 数据剩余验证

java - 不能同时在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean 获取多个包

java - 在自定义 Spring Validator 中无法返回值

java - 使用 keycloak 管理客户端显示用户电子邮件不起作用(UnrecognizedPropertyException)

java - 使用 JERSEY 和 JACKSON 读取 POST 请求数据