복붙노트

[SPRING] 스프링 부트 REST 애플리케이션에서 gzipped 요청 처리하기

SPRING

스프링 부트 REST 애플리케이션에서 gzipped 요청 처리하기

스프링 부트 REST 앱 (1.5.6.RELEASE)이 있습니다. gzip 압축을 들어오고 나가고 싶습니다. 이 문서에 따라 https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html 내가 설정했습니다.

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

하지만 이것은 내 서비스에서 응답을 gzipping하는 경우에만 적용되는 것으로 보입니다 (그리고 이것은 의사가 실제로 "# 응답 압축이 활성화 된 경우"라고합니다).

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

아무도 내 봄 부팅 애플 리케이션에서 요청 압축 해제를 켤 수있는 방법을 알고 있습니까?

EDIT 예제 :

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

gzipped 호출이 다음 메시지와 함께 실패합니다.

{"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":"/"}

해결법

  1. ==============================

    1.server.compression. * 구성 키는 HTTP 응답 압축에만 사용됩니다. 나는 일반적인 솔루션이나 서버가 기본적으로이를 지원하는지 여부를 알지 못합니다.

    server.compression. * 구성 키는 HTTP 응답 압축에만 사용됩니다. 나는 일반적인 솔루션이나 서버가 기본적으로이를 지원하는지 여부를 알지 못합니다.

    그렇게하는 Servlet 필터를 사용하여 지원할 수 있지만 Spring Boot는이 기능을 제공하지 않습니다.

  2. from https://stackoverflow.com/questions/46299674/handling-gzipped-requests-in-a-spring-boot-rest-application by cc-by-sa and MIT license