2017-01-19 48 views
1

我剛剛開始使用Spring Data Rest,並且想要爲在MongoDB中索引文本的字段公開查找器。我有以下索引定義:MongoDBs全文搜索可以與Spring Data REST一起使用嗎?

@TextIndexed 
private String title; 

並驗證索引是否已創建。我已經創建的存儲庫定義查找方法:

public interface ContentRespository extends MongoRepository<Content, String> { 
    public Page<Content> findByTitle(@Param("title") TextCriteria title, @Param("pageable") Pageable pageable); 
} 

通過調用REST API網址:

http://localhost:8080/contents/search/findByTitle?title=test 

我收到以下錯誤:

2017-01-19 13:16:41.831 ERROR 16705 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.repository.support.QueryMethodParameterConversionException: Failed to convert test into org.springframework.data.mongodb.core.query.TextCriteria!] with root cause 

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@org.springframework.data.repository.query.Param org.springframework.data.mongodb.core.query.TextCriteria] 
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:324) ~[spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE] 
.... 

什麼是正確的調用REST API的方式?我找不到任何有關它的文檔。或者如何爲TextCriteria編寫一個轉換器?

回答

0

一段時間後,我回到這一點,並通過定義一個@Query符號取景器找到了答案:

@Query("{$text: {$search: ?0}}") 
public Page<Content> findByTitle(@Param("title") String title, @Param("pageable") Pageable pageable); 
0

與mongo無關 - 但spring rest說你不能將傳入的字符串(從你的web請求)轉換爲暴露的方法參數。你有兩個選擇 - 創建和註冊轉換器或有關包裝方法和將字符串轉換爲所需類型的自己

+0

是的,有沒有任何例子如何寫這樣的轉換器? –

+0

當然。谷歌將幫助找到它。但是,通過某些類包裝內容庫會更容易 –

+0

當然,我可以谷歌它,但我的問題更多的是通過REST API訪問_TextIndexed_字段的默認方式。 _Indexed_字段的其他查找方法正在開箱即用。編寫一個轉換器將是第二個選項 –

相關問題