복붙노트

[SPRING] ClientHttpRequestInterceptor를 사용하여 RestTemplate을 실행하면 GZIP 압축이 두 번 발생합니다.

SPRING

ClientHttpRequestInterceptor를 사용하여 RestTemplate을 실행하면 GZIP 압축이 두 번 발생합니다.

ClientHttpRequestInterceptor를 사용하여 Android 프로젝트의 RestTemplate이 요청한 모든 요청에 ​​Basic Authorization 헤더를 추가하고 있습니다. 또한 Content-Encoding 헤더를 "gzip"으로 설정하여 요청 본문을 압축합니다. RestTemplate에 인터셉터를 추가하면 request.execute 메서드가 두 번 호출됩니다. 시체를 두 번 압축.

인터셉터 :

public class BasicAuthRequestInterceptor implements ClientHttpRequestInterceptor {

/** The base64 encoded credentials */
private final String encodedCredentials;

public BasicAuthRequestInterceptor(final String username, final String password) {
    this.encodedCredentials = new String(Base64.encodeBytes((username + ":" + password).getBytes()));
}

@Override
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body,
        final ClientHttpRequestExecution execution) throws IOException {

    HttpHeaders headers = request.getHeaders();
    headers.add("Authorization", "Basic " + this.encodedCredentials);

    return execution.execute(request, body);
}

}

RestTemplate 설정 :

// Create a new rest template
final RestTemplate restTemplate = new RestTemplate(requestFactory);

// Add authorisation interceptor
final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add(new BasicAuthRequestInterceptor(HTTP_AUTH_USERNAME, HTTP_AUTH_PASSWORD));
restTemplate.setInterceptors(interceptors);

이것이 예상 된 동작이라고 생각하지 않으며이 문제를보고하는 다른 누군가를 찾지 못했습니다. 따라서 인터셉터 구현에 문제가 있습니까? Authorization 헤더를 설정 한 지점에서 Content-Encoding 헤더를 설정하여이 문제를 해결할 수 있지만 바람직하지 않습니다.

spring-android-rest-template 의존성 버전 1.0.1.RELEASE를 사용하고 있습니다.

해결법

    from https://stackoverflow.com/questions/27047161/resttemplate-with-clienthttprequestinterceptor-causes-gzip-compression-twice by cc-by-sa and MIT license