Spring REST 控制器不处理 gzip 压缩输入
Spring REST controller does not handle gzip compressed input
我已根据文档在 application.properties
中设置以下属性,从而设置我的 spring 启动应用程序来处理 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 控制器似乎无法解码输入。我收到 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 引导处理压缩的 JSON 有效负载?
Spring 引导版本是 v2.3.2.RELEASE
。
我认为 server.compression.enabled=true
是关于 response
,而不是 request
。
对于 request
,您可能需要编写自己的 Filter
或使用任何现有的。
在互联网上,我可以找到 和一些可能有帮助的示例。
Spring 不提供此功能,因为它是网络服务器的一部分。
处理此问题的一种方法是提供一个 filter/component 来处理传入请求。
请参阅此答案以获取更多信息和将其实施为 Erik suggested: How to decode Gzip compressed request body in Spring MVC
的提示
我已根据文档在 application.properties
中设置以下属性,从而设置我的 spring 启动应用程序来处理 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 控制器似乎无法解码输入。我收到 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 引导处理压缩的 JSON 有效负载?
Spring 引导版本是 v2.3.2.RELEASE
。
我认为 server.compression.enabled=true
是关于 response
,而不是 request
。
对于 request
,您可能需要编写自己的 Filter
或使用任何现有的。
在互联网上,我可以找到
Spring 不提供此功能,因为它是网络服务器的一部分。 处理此问题的一种方法是提供一个 filter/component 来处理传入请求。
请参阅此答案以获取更多信息和将其实施为 Erik suggested: How to decode Gzip compressed request body in Spring MVC
的提示