# Example with spring

**URL:** https://discuss.elastic.co/t/example-with-spring/72313
**Category:** Elasticsearch
**Created:** [January 20, 2017, 5:07pm UTC](https://discuss.elastic.co/t/example-with-spring/72313 "2017-01-20T17:07:42Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kodo83](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kodo83](https://discuss.elastic.co/u/kodo83)
#### Post date: [January 20, 2017, 5:07pm UTC](https://discuss.elastic.co/t/example-with-spring/72313/1 "2017-01-20T17:07:42Z")

</div>

Hello,  
can you tell me where i can find an example with spring data and elasticsearch.  
i use elasticsearch 5.1.2 and Spring 4.  
i found an example, but when i tried it, exceptions are produced :

Main Classe for tests :  
import org.elasticsearch.common.settings.Settings;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

public class ESMain {

```
@Autowired
private UserRepository repository;

@Autowired
private ElasticsearchTemplate template;

public static void main(String[] args) {
	@SuppressWarnings("resource")
	ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
	ESMain app = (ESMain)ctx.getBean("main");
	Settings s= app.template.getClient().settings();
	Iterable<User> users = app.listAllUsers();
	System.out.print("Done");
}

public Iterable<User> listAllUsers(){
	return repository.findAll();
}

```

spring.xml

```
<context:annotation-config/>

<bean name="main" class="com.es.ESMain"/>
<elasticsearch:repositories base-package="com.es"/>
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300"/>
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
    <constructor-arg name="client" ref="client"/>
</bean>

```

UserRepository:  
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

```
public interface UserRepository extends ElasticsearchRepository<User, String>{
}

```

User:  
package [com.es](http://com.es);

```
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "users", type = "user")
public class User {

	@Id
	private String username;
	private String password;
	private boolean enabled;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public boolean isEnabled() {
		return enabled;
	}
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
}

```

pom : (dependencies)  
  
  
junit  
junit  
3.8.1  
test  
  
  
org.aspectj  
aspectjrt  
${aspectj.version}  
runtime  
  
  
org.aspectj  
aspectjtools  
${aspectj.version}  
  
  
org.springframework  
spring-core  
${spring.version}  
  
  
org.springframework  
spring-context  
${spring.version}  
  
  
org.springframework.data  
spring-data-elasticsearch  
2.0.6.RELEASE  
  
  
org.elasticsearch  
elasticsearch  
5.1.2  
  
  
org.elasticsearch.client  
transport  
5.1.2  
  
  
org.apache.logging.log4j  
log4j-core  
2.7

Exception is :  
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9300}]]  
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)  
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:207)  
.....  
at  
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)  
at com.sun.proxy.$Proxy17.findAll(Unknown Source)  
at com.elasticsearch.esexample.ESMain.main(ESMain.java:26)

thanks.

---

<div class="post-metadata">

### Author: ![kodo83](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kodo83](https://discuss.elastic.co/u/kodo83)
#### Post date: [January 20, 2017, 7:19pm UTC](https://discuss.elastic.co/t/example-with-spring/72313/2 "2017-01-20T19:19:14Z")

</div>

i think the problem is due to the version of

org.springframework.data  
spring-data-elasticsearch  
2.0.6.RELEASE

i have this error on elasticsearch console :  
java.lang.IllegalStateException: Received message from unsupported version: [2.0.0] minimal compatible version is: [5.0.  
0]

---

<div class="post-metadata">

### Author: ![doopey](https://avatars.discourse-cdn.com/v4/letter/d/0ea827/32.png) [@doopey](https://discuss.elastic.co/u/doopey)
#### Post date: [January 22, 2017, 10:18am UTC](https://discuss.elastic.co/t/example-with-spring/72313/3 "2017-01-22T10:18:11Z")

</div>

hi, kodo83

I was also stuck in this problem.  
Have you worked out any approach to solve the problem？  
Thank you~

---

<div class="post-metadata">

### Author: ![kodo83](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kodo83](https://discuss.elastic.co/u/kodo83)
#### Post date: [January 24, 2017, 10:51am UTC](https://discuss.elastic.co/t/example-with-spring/72313/4 "2017-01-24T10:51:18Z")

</div>

Hi doopey,  
Yes, but i returned to the 2.4.1 version of elasticsearch and logstash.

---

<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: [February 21, 2017, 10:51am UTC](https://discuss.elastic.co/t/example-with-spring/72313/5 "2017-02-21T10:51:30Z")

</div>

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