Spring Data REST似乎无法与Elasticsearch一起正常工作

8

我会尽力为您翻译此段文本,内容与IT技术有关。您希望使用Spring Data REST来操作elasticsearch。内置的REST控制器对于POST方法似乎无法正常工作:当我试图发布文档时出现了错误。这个问题很容易重现:

我创建了一个简单的实体:

@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控制器?

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();
    }
}

这个能够兼容的spring-data-elasticsearch的最高版本是多少? - Adrian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接