SpringREST控制器不处理gzip压缩输入
我已通过在SpringREST控制器不处理gzip压缩输入,spring,spring-boot,jackson,spring-restcontroller,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压缩输入后,弹簧座控制器
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压缩输入后,弹簧座控制器似乎无法解码输入。我得到了一个httpmessagegenoteradableexception
。以下是日志中的相关行:
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引导版本是
v2.3.2.RELEASE
我相信server.compression.enabled=true
是关于响应的,而不是请求的。
对于请求
,您可能需要编写自己的过滤器
,或者使用任何现有的过滤器。
查看internet,我可以找到一些可能有用的示例。Spring不提供此功能,因为它是web服务器的一部分。
处理此问题的一种方法是提供一个过滤器/组件来处理输入请求
请参阅此答案,以获得更多信息和建议的实现技巧:关于Spring的一件事是肯定的。它从来不会在开箱即用的情况下做正确的事情。