Spring Data REST does not seem to be working with elasticsearch
I am trying to use Spring Data REST for elasticsearch. The built-in REST controller for POST doesn't seem to be working: I am getting an error when I attempt to post a document. The issue is easy to reproduce: I created a simple entity:
@Document(indexName = "user", type = "user", shards = 1, replicas = 0, refreshInterval = "-1")
public class Customer {
@Id
private String id;
@Field(type = FieldType.String, store = true)
private String firstName;
@Field(type = FieldType.String, store = true)
private String lastName;
// getters and setters are skipped
}
Repository:
public interface UserRepository extends ElasticsearchRepository<User, String> {
}
When I try to get all users I am getting the response:
curl -X GET "http://localhost:9000/users"
{
"_links" : {
"self" : {
"href" : "http://localhost:9000/users{?page,size,sort}",
"templated" : true
},
"search" : {
"href" : "http://localhost:9000/users/search"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}
but when I am trying to add a user:
curl -i -X POST -H "Content-Type:application/json" http://localhost:9000/users -d '{"id":"4e9e62aa-7312-42ed-b8e4-24332d7973cd","firstName":"test","lastName":"test"}'
I am getting an error:
{"cause":null,"message":"PersistentEntity must not be null!"}
There seems to be a Jira ticket opened for this issue without any comments: Jira Issue
I am wondering if it is possible to avoid writing CRUD REST controllers for Spring Data Elasticsearch?
The workaround is to add
@EnableElasticsearchRepositories(repositoryFactoryBeanClass = RestElasticsearchRepositoryFactoryBean.class)
annotation to the application class where RestElasticsearchRepositoryFactoryBean is defined as
@SuppressWarnings("rawtypes")
public class RestElasticsearchRepositoryFactoryBean
extends org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean {
@SuppressWarnings("unchecked")
@Override
public void afterPropertiesSet() {
setMappingContext(new org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext());
super.afterPropertiesSet();
}
}
链接地址: http://www.djcxy.com/p/86432.html