# Java High Level Rest Client to Elasicsearch

**URL:** <https://discuss.elastic.co/t/java-high-level-rest-client-to-elasicsearch/250929>\
**Category:** Elasticsearch\
**Created:** [October 5, 2020, 6:21am UTC](https://discuss.elastic.co/t/java-high-level-rest-client-to-elasicsearch/250929 "2020-10-05T06:21:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pathfinder225](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@pathfinder225](https://discuss.elastic.co/u/pathfinder225)\
**Post date:** [October 5, 2020, 6:21am UTC](https://discuss.elastic.co/t/java-high-level-rest-client-to-elasicsearch/250929/1 "2020-10-05T06:21:41Z")

</div>

Hi

I am using Java High Level Rest client on Spring boot to perform Search API operations.

Our Elasticsearch is protected with https and basic(user/pwd) authentication.  
I'm receiving below exception ,

```auto
javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

```

Code below,

```auto
	@Bean
	public RestHighLevelClient initializeClient() {
		final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

		RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, "https")).setHttpClientConfigCallback(
				httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
		
		builder.setRequestConfigCallback(
			    new RestClientBuilder.RequestConfigCallback() {
			        @Override
			        public RequestConfig.Builder customizeRequestConfig(
			                RequestConfig.Builder requestConfigBuilder) {
			            return requestConfigBuilder.setSocketTimeout(10000); 
			        }
			    });
		
		RestHighLevelClient client = new RestHighLevelClient(builder);

		return client;
	}

```

Do i need to do something more to connect to a https elastic search service , if so how would solution varies across dev and prod. DO i need to install ssl certificate of elasticsearch into my jvm?

---

<div class="post-metadata">

**Author:** ![Wolfram\_Haussig](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wolfram_haussig/32/70528_2.png) [@Wolfram\_Haussig](https://discuss.elastic.co/u/Wolfram_Haussig)\
**Post date:** [October 5, 2020, 6:48am UTC](https://discuss.elastic.co/t/java-high-level-rest-client-to-elasicsearch/250929/2 "2020-10-05T06:48:09Z")

</div>

Hi,

This is not Elastic specific: Your Java installation does not trust your Elastic certificate. Depending on your java version you can find the trusted certificates under `$JAVA_HOME/lib/security/cacerts` or `$JAVA_HOME/jre/lib/security/cacerts`. This file should include your any certificate of your cert chain.

Best regards  
Wolfram

---

<div class="post-metadata">

**Author:** ![pathfinder225](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@pathfinder225](https://discuss.elastic.co/u/pathfinder225)\
**Post date:** [October 5, 2020, 6:52am UTC](https://discuss.elastic.co/t/java-high-level-rest-client-to-elasicsearch/250929/3 "2020-10-05T06:52:29Z")

</div>

@Wolfram_Haussig

Appreciate your response.

In prod also do we need to do same?

I have tested from postman ([https://10.23.211.244:9002/?pretty](https://10.23.211.244:9002/?pretty)) and it worked after turning off ssl certificate verification in postman settings. Can i do something similar from java rest high level client rather than installing the certificate in jvm. I'm new to working with ssl .

---

<div class="post-metadata">

**Author:** ![Wolfram\_Haussig](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wolfram_haussig/32/70528_2.png) [@Wolfram\_Haussig](https://discuss.elastic.co/u/Wolfram_Haussig)\
**Post date:** [October 5, 2020, 6:59am UTC](https://discuss.elastic.co/t/java-high-level-rest-client-to-elasicsearch/250929/4 "2020-10-05T06:59:38Z")

</div>

While it is possible(see [here](https://stackoverflow.com/questions/4663147/is-there-a-java-setting-for-disabling-certificate-validation) for example) I absolutely do **NOT** recommend that!

---

<div class="post-metadata">

**Author:** ![pathfinder225](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@pathfinder225](https://discuss.elastic.co/u/pathfinder225)\
**Post date:** [October 6, 2020, 2:17pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-to-elasicsearch/250929/5 "2020-10-06T14:17:18Z")

</div>

@Wolfram_Haussig Thanks got it working after installing ssl certificate in java keystore.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 3, 2020, 2:17pm UTC](https://discuss.elastic.co/t/java-high-level-rest-client-to-elasicsearch/250929/6 "2020-11-03T14:17:23Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
