8

我正在尝试将 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 控制器?

4

1 回答 1

4

解决方法是添加

@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();
    }
}
于 2015-07-08T15:08:07.807 回答