我正在使用spring boot 1.1.5构建一个MongoDb rest应用程序。我创建了一个存储库:
@RepositoryRestResource(collectionResourceRel = "SourceProductV1Repository", path = "SourceProductV1Repository")
public interface SourceProductV1Repository extends MongoRepository<SourceProductV1Model, String> {
public List<SourceProductV1Model> findByUser (@Param("user") MongoUser user);
}
我定义了一个将String转换为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)
)
我做错了什么?也许在转换器中期待某种其他类型的目标?
答案 0 :(得分:6)
我修改了以下列方式创建bean:
@Configuration
public class MongoConvertersConfiguration {
@Resource(name = "defaultConversionService")
private GenericConversionService genericConversionService;
@Bean
public String2MongoUserConverter string2MongoUserConverter(){
String2MongoUserConverter string2MongoUserConverter = new String2MongoUserConverter();
genericConversionService.addConverter(string2MongoUserConverter);
return string2MongoUserConverter;
}
}
答案 1 :(得分:0)
经过多次尝试,我找到了这个解决方案
@PostConstruct
public void init(){
GenericConversionService conversionService = (GenericConversionService) DefaultConversionService.getSharedInstance();
conversionService.addConverter(new JPAConverter());
}
支持Converter和GenericConvert。