prosource

Spring Boot REST 응용 프로그램에서 gzip 요청 처리

probook 2023. 8. 1. 20:38
반응형

Spring Boot REST 응용 프로그램에서 gzip 요청 처리

Spring Boot REST 앱(1.5.6)을 가지고 있습니다.해제).나는 gzip 압축을 들어오고 나가고 싶습니다. 문서에 따라 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html 을 설정했습니다.

server.compression.enabled=true
server.compression.mime-types=...

그러나 이는 서비스의 응답을 gzip하는 경우에만 적용되는 것 같습니다(그리고 이것은 의사가 실제로 "응답 압축이 활성화된 경우"라고 말하는 것입니다)

문제는 들어오는 gzip 요청이 압축 해제되지 않아 JSON 구문 분석 오류가 발생한다는 것입니다.

스프링 부트 앱에서 요청 압축 해제를 설정하는 방법을 아는 사람이 있습니까?

편집 예:

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");
   }
}

컬을 사용하여 테스트:

$ 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

gzip된 호출이 실패하고 다음 메시지가 표시됩니다.

{"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 응답 압축에 대한 것입니다.일반적인 솔루션이나 서버가 기본적으로 지원하는 솔루션에 대해서는 알지 못합니다.

이 기능을 수행하는 서블릿 필터를 사용하여 이러한 기능을 지원할 수 있지만 스프링 부트는 이러한 기능을 제공하지 않습니다.

언급URL : https://stackoverflow.com/questions/46299674/handling-gzipped-requests-in-a-spring-boot-rest-application

반응형