3

我已经在我的 Spring Boot 应用程序上启用了内容压缩,并且每个 API 的响应都被压缩了,但是如果我使用这些 APIS,RestTemplate我会得到类似的内容

"\u001F�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�}��8��¨OtD���1��]�m�mo��v�_LlP\u0014J�4E��(�����C�:\u0012<D\u0010����\b۲��\u0004\u0012@\"

但是,如果我使用HttpClientBuilder.create().build()创建RestTemplate对象并headers.set("Content-Encoding", "gzip"); headers.set("Accept-Encoding", "gzip");在为交换请求设置标头时添加,我会收到字符串格式的正确 json 对象,因为 API 本身返回一个字符串。

但是我在使用 访问这些 API 时遇到了同样的问题AsyncRestTemplate,即使我AsyncRestTemplate使用我已经存在的RestTemplate对象创建对象也是如此。

我拥有的一种方法是自己实现线程池,然后使用 来点击请求RestTemplate,但我想知道是否有任何方法可以在不实现线程机制的情况下获取 json 对象而不是压缩内容。

下面是完整的代码

public class RestUtil {

    @Resource RestTemplate restTemplate;
    @Resource AsyncRestTemplate asyncRestTemplate;

    private final Log log = LogFactory.getLog(getClass());

    public String getSyncResponse(HttpServletRequest request) {
        final String URL = "not including url";

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(URL);

        HttpEntity<String> entity = new HttpEntity<>(null, getHeaders(request));
        try {
            ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), GET, entity, String.class);
            System.out.println("==== sync response ====");
            System.out.println(response.getBody());
            System.out.println("========");
            return response.getBody();
        } catch (Exception ex) {
            log.error("Error in connecting to codebook server ", ex);
        }
        return null;
    }

    public List<String> getAsyncResponse(HttpServletRequest request) {

        List<String> urls = new ArrayList<>();
        urls.add("not including url");
        urls.add("not including url");

        HttpEntity<String> entity = new HttpEntity<>(null, getHeaders(request));
        try {

            ArrayList<Future<ResponseEntity<String>>> futures = new ArrayList<>();

            for (String url : urls) {
                ListenableFuture<ResponseEntity<String>> listenableFuture = asyncRestTemplate.exchange(url, GET, entity, String.class);
                listenableFuture.addCallback(response -> log.info("Success"), ex -> log.error("Request failed", ex));
                futures.add(listenableFuture);
            }

            List<String> responses = new ArrayList<>();
            for (Future<ResponseEntity<String>> future : futures) {
                responses.add(future.get().getBody());
            }

            System.out.println("==== async response ====");
            System.out.println(responses);
            System.out.println("========");

            return responses;

        } catch (Exception ex) {
            log.error("Error in connecting to server ", ex);
        }
        return null;
    }

    private HttpHeaders getHeaders(HttpServletRequest request) {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", APPLICATION_JSON_UTF8_VALUE);
        headers.set("Content-Type", APPLICATION_JSON_UTF8_VALUE);
        headers.set("Content-Encoding", "gzip");
        headers.set("Accept-Encoding", "gzip");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String header = headerNames.nextElement();
            Enumeration<String> headerList = request.getHeaders(header);
            headers.put(header, Collections.list(headerList));
        }

        return headers;
    }
}


@Configuration
class HttpConfig {

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate(httpRequestFactory());

        MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter();
        jsonHttpMessageConverter.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        restTemplate.getMessageConverters().add(jsonHttpMessageConverter);

        return restTemplate;
    }

    @Bean
    public ClientHttpRequestFactory httpRequestFactory() {
        return new HttpComponentsClientHttpRequestFactory(httpClient());
    }

    @Bean
    public CloseableHttpClient httpClient() {
        return HttpClientBuilder.create().build();
    }

    @Bean
    public AsyncRestTemplate asyncRestTemplate(AsyncClientHttpRequestFactory asyncHttpRequestFactory, RestTemplate restTemplate) {
        return new AsyncRestTemplate(asyncHttpRequestFactory, restTemplate);
    }

    @Bean
    public AsyncClientHttpRequestFactory asyncHttpRequestFactory() {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setTaskExecutor(new SimpleAsyncTaskExecutor());
        return requestFactory;
    }

}
4

2 回答 2

3

一个简单的解决方法是添加一个带有 AsyncRestTemplate 的 ResponseInterceptor,它会自动解压缩响应。您收到的 ListenableFuture 将已经有解压缩的响应。这是一个例子:

AsyncRestTemplate getAsyncRestTemplate() {
    final HttpComponentsAsyncClientHttpRequestFactory httpComponentsAsyncClientHttpRequestFactory =
            new HttpComponentsAsyncClientHttpRequestFactory();

    final AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate(httpComponentsAsyncClientHttpRequestFactory);
    asyncRestTemplate.setInterceptors(Collections.singletonList((httpRequest, bytes, asyncClientHttpRequestExecution) -> {
        if (!httpRequest.getHeaders().containsKey(HttpHeaders.ACCEPT_ENCODING)) {
            httpRequest.getHeaders().set(HttpHeaders.ACCEPT_ENCODING, "gzip");
        }
        final ListenableFuture<ClientHttpResponse> future = asyncClientHttpRequestExecution.executeAsync(httpRequest, bytes);
        return new ListenableFutureAdapter<ClientHttpResponse, ClientHttpResponse>(future) {
            @Override
            protected ClientHttpResponse adapt(ClientHttpResponse clientHttpResponse) throws ExecutionException {
                return new InflatedClientHttpResponse(clientHttpResponse);
            }
        };
    }));
    return asyncRestTemplate;

}

public class InflatedClientHttpResponse implements ClientHttpResponse {
    private final ClientHttpResponse clientHttpResponse;
    private final Boolean isCompressed;

    public InflatedClientHttpResponse(ClientHttpResponse clientHttpResponse) {
        this.clientHttpResponse = clientHttpResponse;
        final HttpHeaders httpHeaders = clientHttpResponse.getHeaders();

        final List<String> contentEncoding = httpHeaders.get(HttpHeaders.CONTENT_ENCODING);
        if (contentEncoding != null && contentEncoding.contains("gzip")) {
            isCompressed = true;
        } else {
            isCompressed = false;
        }

        httpHeaders.remove(HttpHeaders.CONTENT_ENCODING);
        httpHeaders.remove(HttpHeaders.CONTENT_LENGTH);
    }

    @Override
    public HttpStatus getStatusCode() throws IOException {
        return clientHttpResponse.getStatusCode();
    }

    @Override
    public int getRawStatusCode() throws IOException {
        return clientHttpResponse.getRawStatusCode();
    }

    @Override
    public String getStatusText() throws IOException {
        return clientHttpResponse.getStatusText();
    }

    @Override
    public void close() {
        clientHttpResponse.close();
    }

    @Override
    public InputStream getBody() throws IOException {
        if (isCompressed) {
            return new GZIPInputStream(clientHttpResponse.getBody());
        }
        return clientHttpResponse.getBody();
    }

    @Override
    public HttpHeaders getHeaders() {
        return clientHttpResponse.getHeaders();
    }
}
于 2017-06-17T06:51:46.847 回答
0

我建议您不要手动进行压缩,而只是告诉 Spring 为您执行此操作(实际上压缩将由 Web 服务器(例如 Tomcat)完成,但 Spring 会为您执行此操作)。通常在 Spring 中诸如压缩之类的东西,编码可以通过单个属性打开

请看http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#how-to-enable-http-response-compression

于 2017-02-03T07:28:00.593 回答