Requirement is to have a common service to make rest call with a third party api . To have the configuration on HTTPS we basically require to have certificates initialized to out SSL context , (Previous post on how to setup your ssl bean
in spring context
) Register the autowired bean in your class .
@Autowired(required=false)
private MySSLSocketFactory socketFactory;
Code to make the rest call in production (HTTPS) environment.
sslContext = socketFactory.getSSLContext();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
restTemplate.setRequestFactory(requestFactory);
HttpEntity> restRequest = new HttpEntity>(map, createHeaders("userId","userPwd")));
ResponseEntity response= restTemplate.postForEntity(URL, restRequest , String.class );