Getting bean creation issue with java 17,spring boot 3.0 and Elastic Search 8.6 here is my client class which is used to initiate elastic connection:
@Slf4j
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.es.repository")
@ComponentScan(basePackages = { "com.es","com.es.model","com.es.service","com.es.config" })
public class ESJavaClient
{
@Value("${spring.elasticsearch.url}")
private String url;
@Value("${spring.elasticsearch.url.noport}")
private String urlWithoutPort;
@Value("${spring.elasticsearch.port}")
private String port;
@Value("${spring.elasticsearch.protocol}")
private String protocol;
@Value("${spring.elasticsearch.ssl.truststore.path}")
private String truststorePath;
@Value("${spring.elasticsearch.ssl.truststore.password}")
private String truststorePassword;
@Value("${spring.elasticsearch.ssl.keystore.path}")
private String keystorePath;
@Value("${spring.elasticsearch.ssl.keystore.password}")
private String keystorePassword;
@Value("${spring.elasticsearch.username}")
private String username;
@Value("${spring.elasticsearch.password}")
private String password;
private static ElasticsearchAsyncClient asyncClient;
@Autowired
private ElasticsearchOperations elasticsearchTemplate;
@Autowired
private RestClient restClient;
@Autowired
private ElasticsearchTransport transport;
@Autowired
private ElasticsearchClient elasticsearchClient;
@Bean
public ElasticsearchClient elasticsearchClient() {
RestClient restClient = restClient();
JacksonJsonpMapper jacksonJsonpMapper = new JacksonJsonpMapper();
ElasticsearchTransport transport = new RestClientTransport(restClient, jacksonJsonpMapper);
//asyncClient = new ElasticsearchAsyncClient(transport);
ElasticsearchClient elasticsearchClient = new ElasticsearchClient(transport);
return elasticsearchClient;
}
@Bean
public RestClient restClient() {
return restClientBuilder().build();
}
@Bean
public RestClientBuilder restClientBuilder() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
HttpHost ht = new HttpHost(urlWithoutPort, Integer.parseInt(port), protocol);
if (protocol != null && protocol.equalsIgnoreCase("https")) {
return RestClient.builder(ht).setRequestConfigCallback(
requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(180000).setSocketTimeout(900000))
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider).setSSLContext(getSSL()));
}
return RestClient.builder(ht)
.setRequestConfigCallback(
requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(180000).setSocketTimeout(900000))
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder);
}
/*@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(elasticsearchClient);
}*/
/*@PreDestroy
public void cleanup() {
try {
log.info("Closing the ES REST client");
transport.close();
} catch (IOException e) {
log.error("Problem occurred when closing the ES REST client", e);
}
}*/
private SSLContext getSSL() {
try {
File key = ResourceUtils.getFile("classpath:keystore.jks");
File trust = ResourceUtils.getFile("classpath:truststore.jks");
return SSLContexts.custom()
.loadKeyMaterial(key, keystorePassword.toCharArray(), keystorePassword.toCharArray())
.loadTrustMaterial(trust, truststorePassword.toCharArray()).build();
} catch (Exception e) {
log.error("SSLContext set failed");
log.error(e.getMessage());
return null;
}
}
}
2 Modelclass :
@Getter
@Setter
public class TestModelDTO {
private String name;
private String address;
}
Test Repositry Interface
@Repository
public interface TestRepository extends ElasticsearchRepository<TestRepositoryModel, String>{
public TestRepositoryModel findByName(String itemId);
}
- TestRepositoryModel class
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "#{@environment.getProperty('test.model.project.index')}")
public class TestRepositoryModel {
@Id
@Field(type= FieldType.Keyword)
private String id;
@Field(type= FieldType.Keyword)
private String name;
@Field(type= FieldType.Keyword)
private String address;
}
When i start the server i am getting the below error
Spring not able to create beans for repository classes, I am getting bean creation issue, when include Repository related class in my project getting bean creation issue. I am using Intellij 2022.2.1.we have migrated java 11 to 17,spring boot from 2.5 to 3.0 and elastic search 7.7 to 8.6
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-08-09T17:10:57.275+05:30 ERROR 25004 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testServiceImpl': Unsatisfied dependency expressed through field 'testRepository': Error creating bean with name 'testRepository' defined in com.optum.bpa.es.repository.TestRepository defined in @EnableElasticsearchRepositories declared on ESJavaClient: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception
at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:155) ~[elasticsearch-java-8.8.2.jar:na]
at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.create(ElasticsearchIndicesClient.java:266) ~[elasticsearch-java-8.8.2.jar:na]
at org.springframework.data.elasticsearch.client.elc.IndicesTemplate.lambda$doCreate$0(IndicesTemplate.java:138) ~[spring-data-elasticsearch-5.0.5.jar:5.0.5]
at org.springframework.data.elasticsearch.client.elc.ChildTemplate.execute(ChildTemplate.java:71) ~[spring-data-elasticsearch-5.0.5.jar:5.0.5]
... 56 common frames omitted
Caused by: org.elasticsearch.client.ResponseException: method [PUT], host [{Cluster-URL}:443], URI [/test-model-project], status line [HTTP/1.1 400 Bad Request]
... 61 common frames omitted
we tried below solution as temporary fix as elastic cluster upgrade to 8.16: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-compatibility.html
but we are moving elastic client as permanent fix. Can anybody please suggest solution for above error.