Spring 的 AsyncRestTemplate 不适用于压缩内容,获取压缩内容而不是 json 对象

Posted

技术标签:

【中文标题】Spring 的 AsyncRestTemplate 不适用于压缩内容,获取压缩内容而不是 json 对象【英文标题】:Spring's AsyncRestTemplate not working with zipped content, Getting compressed content instead of json object 【发布时间】:2017-06-20 11:49:25 【问题描述】:

我在我的 Spring Boot 应用上启用了内容压缩,每个 API 的响应都被压缩了,但是如果我使用 RestTemplate 访问这些 API,我会得到类似

的内容
"\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 本身返回一个字符串。

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

我的一种方法是自己实现线程池,然后使用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;
    


【问题讨论】:

【参考方案1】:

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

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

【讨论】:

是的,我不是手动做的,Spring 正在帮我做我刚刚在我的 application.properties 文件中编写了这些属性 server.compression.enabled=true server.compression.mime-types=application /javascript,application/x-javascript,application/json,application/xml,text/html,text/xml,text/css,text/plain【参考方案2】:

一个简单的解决方法是添加一个带有 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();
    

【讨论】:

以上是关于Spring 的 AsyncRestTemplate 不适用于压缩内容,获取压缩内容而不是 json 对象的主要内容,如果未能解决你的问题,请参考以下文章

学习笔记——Spring简介;Spring搭建步骤;Spring的特性;Spring中getBean三种方式;Spring中的标签

Spring全家桶笔记:Spring+Spring Boot+Spring Cloud+Spring MVC

Spring--Spring入门

Spring:Spring介绍

Spring浅析Spring框架的搭建

Spring DAO vs Spring ORM vs Spring JDBC