7

Sto cercando di utilizzare Spring Data REST per elasticsearch. Il controller REST incorporato per POST non sembra funzionare: sto ricevendo un errore quando tento di pubblicare un documento. Il problema è facile da riprodurre: Ho creato una semplice entità:Spring Data REST non sembra funzionare con elasticsearch

@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> { 
} 

Quando provo ad ottenere tutti gli utenti sto ottenendo la risposta:

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 
} 
} 

ma quando sto cercando di aggiungere un utente:

curl -i -X POST -H "Content-Type:application/json" http://localhost:9000/users -d '{"id":"4e9e62aa-7312-42ed-b8e4-24332d7973cd","firstName":"test","lastName":"test"}' 

sto ottenendo un errore:

{"cause":null,"message":"PersistentEntity must not be null!"} 

Sembra che ci sia un biglietto Jira aperto per questo problema senza alcun commento: Jira Issue

Mi chiedo se è possibile evitare di scrivere i controllori CRUD riposo per Spring Data Elasticsearch?

risposta

4

La soluzione è aggiungere

@EnableElasticsearchRepositories(repositoryFactoryBeanClass = RestElasticsearchRepositoryFactoryBean.class) 

annotazione classe di applicazione in cui è definito come 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(); 
    } 
} 
+0

Works! Dank you. – Tarion

Problemi correlati