MongoDBs全文搜索可以与Spring Data REST一起使用吗?

时间:2017-01-19 12:32:07

标签: mongodb spring-data-rest

我刚刚开始使用Spring Data Rest,并希望为MongoDB中的文本索引字段公开finder。我有以下索引定义:

@TextIndexed
private String title;

并验证是否已创建索引。我在存储库定义上创建了一个finder方法:

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

通过调用REST API URL:

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编写转换器?

2 个答案:

答案 0 :(得分:0)

与mongo无关 - 但是Spring rest告诉你,它无法将传入的字符串(来自您的Web请求)转换为公开的方法参数。您有2个选项 - 创建并注册转换器或包装方法,并自行将字符串转换为所需类型

答案 1 :(得分:0)

过了一段时间后,我回到这里,通过使用@Query符号定义查找器找到了答案:

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