Spring Data REST似乎不适用于elasticsearch
我正在尝试使用Spring Data REST进行弹性搜索。 用于POST的内置REST控制器似乎不工作:当我尝试发布文档时出现错误。 这个问题很容易重现:我创建了一个简单的实体:
@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
}
库:
public interface UserRepository extends ElasticsearchRepository<User, String> {
}
当我尝试让所有用户获得响应时:
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
}
}
但是当我试图添加一个用户时:
curl -i -X POST -H "Content-Type:application/json" http://localhost:9000/users -d '{"id":"4e9e62aa-7312-42ed-b8e4-24332d7973cd","firstName":"test","lastName":"test"}'
我收到一个错误:
{"cause":null,"message":"PersistentEntity must not be null!"}
这个问题似乎有一个Jira票据没有任何评论:Jira Issue
我想知道是否有可能避免为Spring Data Elasticsearch编写CRUD REST控制器?
解决方法是添加
@EnableElasticsearchRepositories(repositoryFactoryBeanClass = RestElasticsearchRepositoryFactoryBean.class)
将RestElasticsearchRepositoryFactoryBean定义为应用程序类的注释
@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/86431.html
上一篇: Spring Data REST does not seem to be working with elasticsearch