I just started playing around with Spring Data Rest and wanted to expose a finder for a field which is text indexed in MongoDB. I have the following index definition:
@TextIndexed
private String title;
and verified that the index is created. I have created a finder method on the repository definition:
public interface ContentRespository extends MongoRepository<Content, String> {
public Page<Content> findByTitle(@Param("title") TextCriteria title, @Param("pageable") Pageable pageable);
}
By calling the REST API URL:
http://localhost:8080/contents/search/findByTitle?title=test
I get the following error:
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]
....
What would be the correct way to call the REST API? I can't find any documentation about it. Or how would it be possible to write a converter for TextCriteria?
After some time I came back to this and found the answer by defining a finder with a @Query notation:
@Query("{$text: {$search: ?0}}")
public Page<Content> findByTitle(@Param("title") String title, @Param("pageable") Pageable pageable);
Not related to mongo - but spring rest says you that it can not convert incoming string ( from you web request ) to exposed method parameter. You have 2 options - create and register converter or wrap method in question and convert strings to required types yourself