Spring REST controller does not handle gzip compressed input

Viewed 1202

I have set up my spring boot application to handle HTTP server compression by setting the following properties in application.properties according to the documentation:

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

Nevertheless, upon receiving gzip compressed input the Spring REST controller does not seem to be able to decode the input. I am getting a HttpMessageNotReadableException. Here are the relevant lines from the log:

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"]]

Is there some additional configuration required to make Spring Boot handle the compressed JSON payload?

The Spring Boot version is v2.3.2.RELEASE.

2 Answers

I believe server.compression.enabled=true is about the response, not the request. For the request, you would probably need to write your own Filter or use any existing ones. Looking on internet, I could find this with some examples that may help.

Spring does not offer this feature, as it's part of the web server. One way you can handle this issue is to supply a filter/component that handles incomming requests.

Please see this answer for more information and tips in implementing it as Erik suggested: How to decode Gzip compressed request body in Spring MVC

Related