我正在用spring boot 1.1.5構建一個MongoDb rest應用程序。我創建了一個信息庫:找不到能夠從java.lang.String類型轉換爲類型@ org.springframework.data.repository.query.Param的轉換器
@RepositoryRestResource(collectionResourceRel = "SourceProductV1Repository", path = "SourceProductV1Repository")
public interface SourceProductV1Repository extends MongoRepository<SourceProductV1Model, String> {
public List<SourceProductV1Model> findByUser (@Param("user") MongoUser user);
}
我定義了一個轉換器,以字符串轉換爲MongoUser(轉換器在Spring上下文正確註冊):
@Component
public class String2MongoUserConverter implements Converter<String, MongoUser>{
private static final Log LOGGER = LogFactory.getLog(String2MongoUserConverter.class.getName());
@Override
public MongoUser convert(String s) {
return null;
}
}
所以,當我嘗試訪問http://localhost:8080/SourceProductV1Repository/search/findByUser?user=test
我得到以下錯誤:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type java.lang.String to type @org.springframework.data.repository.query.Param com.ddelizia.model.MongoUser
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:291)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:177)
at org.springframework.data.rest.core.invoke.ReflectionRepositoryInvoker.prepareParameters(ReflectionRepositoryInvoker.java:265)
at org.springframework.data.rest.core.invoke.ReflectionRepositoryInvoker.invokeQueryMethod(ReflectionRepositoryInvoker.java:230)
at org.springframework.data.rest.webmvc.RepositorySearchController.executeQueryMethod(RepositorySearchController.java:236)
at org.springframework.data.rest.webmvc.RepositorySearchController.executeSearch(RepositorySearchController.java:146)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
)
我做錯了什麼?也許它在轉換器中期待某種其他類型的目標?
你確定轉換器是否正確註冊?你如何註冊轉換器?爲什麼轉換器有一個'@ Repository'註釋? – Jesper 2014-09-03 06:53:10
這是我的錯誤..無論如何,我註冊爲'@組件'(我打印所有實例化的豆,我看到正在運行),但我得到了同樣的錯誤..可以是'@參數'anotation(我使用Java 7) – ddelizia 2014-09-03 07:54:29
我不認爲只在應用程序上下文中註冊轉換器就足夠了。查看文檔:[使用自定義轉換器覆蓋默認映射](http://docs.spring.io/spring-data/data-mongo/docs/current/reference/html/mongo.core.html#mongo.custom-converters ) – Jesper 2014-09-03 09:05:35