我已经在我的 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;
}
}