美好的一天,我的restTemplate代码出现问题,我请求需要TLS1.2的Web服务,并使用CloseableHttpClient将ssl添加到我的代码中并且可以正常工作,但是现在我需要为日志添加拦截器请求和响应,与此有关。 响应主体是一个流,如果您在拦截器中读取它,则RestTemplate将无法将其反序列化为对象模型。换句话说,我得到的对象为空。 我找到的解决方案是对restTemplate使用BufferingClientHttpRequestFactory,但是我不知道如何添加TLS1.2
这是我的代码:
构造函数
HttpComponentsClientHttpRequestFactory factory;
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);
SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext,
new AllowAllHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("https", connectionFactory).build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
factory = new HttpComponentsClientHttpRequestFactory(httpClient);
} catch (Exception e) {
e.printStackTrace();
}
RestTemplate调用
BargainFinderMaxRS bargainFinderMaxRS = new BargainFinderMaxRS();
try {
RestTemplate restTemplate = new RestTemplate(factory);
HttpHeaders headers = createHttpHeadersForBargainFinderMax(authentication);
HttpEntity<BargainFinderMaxRQ> request =
new HttpEntity<BargainFinderMaxRQ>(bargainFinderMaxRQ, headers);
restTemplate.setInterceptors(Collections.singletonList(
new ClientHttpInterceptor()));
bargainFinderMaxRS =
restTemplate.postForObject(bargainFinderMaxProperties.getUrl(), request, BargainFinderMaxRS.class);
} catch (Exception e) {
e.printStackTrace();
}
return bargainFinderMaxRS;
在此代码中,bargainFinderMaxRS返回null 有人可以帮助我吗?