How to learn first-hand information about RestTemplate instead of searching for second-hand information written by others online?
How to learn restemplate on the Spring official website, but there is too little information on HTTPS on the Spring official website. How did others write code online for HTTPS and where did they refer to it?
I tried to learn restemplate on the Spring official website, but there is too little information available on the website, only a brief introduction。
The following code is the code I searched online about setting HTTPS for restemplate. How did the person who wrote this code know to write it this way? I have no other choice but to search and can only copy someone else's code:
@Configuration
public class RestTemplateManager {
@Bean
public RestTemplate httpsRestTemplate(HttpComponentsClientHttpRequestFactory httpsFactory) {
RestTemplate restTemplate = new RestTemplate(httpsFactory);
restTemplate.setErrorHandler(new ResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) {
return false;
}
@Override
public void handleError(ClientHttpResponse clientHttpResponse) {
}
});
return restTemplate;
}
@Bean(name = "httpsFactory")
public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory() throws Exception {
CloseableHttpClient httpClient = acceptsUntrustedCertsHttpClient();
HttpComponentsClientHttpRequestFactory httpsFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
httpsFactory.setReadTimeout(SemanticConfig.INSTANCE.getSocketTimeout());
httpsFactory.setConnectTimeout(SemanticConfig.INSTANCE.getConnectTimeout());
return httpsFactory;
}
public static CloseableHttpClient acceptsUntrustedCertsHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
b.setSSLContext(sslContext);
// don't check Host names, either.
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
// create a SSL Socket Factory, to use our weakened "trust strategy" and create a Registry, to register it.
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslSocketFactory)
.build();
// create connection-manager using registry allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connMgr.setMaxTotal(200);
connMgr.setDefaultMaxPerRoute(100);
b.setConnectionManager(connMgr);
return b.build();
}
}