No converter found capable of converting from type java.lang.String to type @org.springframework.data.repository.query.Param

假如想象 提交于 2019-11-30 20:59:03

问题


I'm building a MongoDb rest application with spring boot 1.1.5. I have created a repository:

@RepositoryRestResource(collectionResourceRel = "SourceProductV1Repository", path = "SourceProductV1Repository")
public interface SourceProductV1Repository extends MongoRepository<SourceProductV1Model, String> {

    public List<SourceProductV1Model> findByUser (@Param("user") MongoUser user);

}

I Defined a converter to convert String to MongoUser (the converter is correctly registered in the spring context):

@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;
    }
}

So when I try to access http://localhost:8080/SourceProductV1Repository/search/findByUser?user=test

I get the following error:

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)

)

What I'm doing wrong? maybe it is expecting some kind of other type as target in the converter?


回答1:


I fixed creating the bean in the following way:

@Configuration
public class MongoConvertersConfiguration {

    @Resource(name = "defaultConversionService")
    private GenericConversionService genericConversionService;

    @Bean
    public String2MongoUserConverter string2MongoUserConverter(){
        String2MongoUserConverter string2MongoUserConverter = new String2MongoUserConverter();
        genericConversionService.addConverter(string2MongoUserConverter);
        return string2MongoUserConverter;
    }

}



回答2:


After many try, i've found this solution

    @PostConstruct
public void init(){
    GenericConversionService conversionService = (GenericConversionService) DefaultConversionService.getSharedInstance();
    conversionService.addConverter(new JPAConverter());
}

Converter and GenericConvert are supported.



来源:https://stackoverflow.com/questions/25628063/no-converter-found-capable-of-converting-from-type-java-lang-string-to-type-org

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!