ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type

对接接口的时候发现这个错误,经过查找发现是在Repository中的JPA nativeQuery中,直接使用了对象返回,但是nativeQuery返回的东西应该是一个Map 、 List<Map> 或者 [java.lang.Object[]]之类的不能直接转化为对象。

出问题的地方:

 @Query(value = "SELECT rule.* "
            +" FROM  device device, device_consume_rule rule, device_consume_rule_device relaction"
            +" WHERE device.id = relaction.device_id"
            +" AND rule.id = relaction.device_consume_rule_id"
            +" AND device.id = (:deviceId)"
            +" AND rule.device_consume_rule_status = 1"
            +" AND rule.is_deleted = 0"
            +" AND relaction.is_deleted = 0",
            nativeQuery = true)
    List<DeviceConsumeRule> findConsumeRule(@Param("deviceId") Integer deviceId);

处理办法:

 List<DeviceConsumeRule> findConsumeRule(@Param("deviceId") Integer deviceId);

修改为:

 List<Map<String,Object>> findConsumeRule(@Param("deviceId") Integer deviceId);

然后在Service里面在处理成对象即可。

详细报错:

org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [com.cobi.iccard.domain.DeviceConsumeRule] for value '{10015, 10001, 10007, 规则, 9999.00, 1, balance, {"10000":"10019"}, 1, 2018-08-09 11:29:04.0, 2018-08-09 11:32:16.0, 0}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [com.cobi.iccard.domain.DeviceConsumeRule]
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:46)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:174)
    at org.springframework.data.repository.query.ResultProcessor$ProjectingConverter.convert(ResultProcessor.java:293)
    at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.lambda$and$0(ResultProcessor.java:213)
    at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:224)
    at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:152)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:141)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:125)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:590)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy165.findConsumeRule(Unknown Source)
    at com.cobi.iccard.service.impl.CardCapitalServiceImpl.consume(CardCapitalServiceImpl.java:134)
    at com.cobi.iccard.service.impl.CardCapitalServiceImpl$$FastClassBySpringCGLIB$$af4c12fc.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)【info】给消费机:F:0&E:30&G:Failed to &D:1

    at com.cobi.iccard.service.impl.CardCapitalServiceImpl$$EnhancerBySpringCGLIB$$d97cb4e9.consume(<generated>)
    at com.cobi.iccard.service.impl.YHCServiceImpl.freeConsume(YHCServiceImpl.java:164)
    at com.cobi.iccard.utils.YHCCode.codeAnalysis(YHCCode.java:106)
    at com.cobi.iccard.socket.SocketServer.handle(SocketServer.java:108)
    at com.cobi.iccard.socket.SocketServer.startSocketServer(SocketServer.java:63)
    at com.cobi.iccard.socket.MyApplicationRunner.run(MyApplicationRunner.java:20)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:788)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:778)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
    at com.cobi.iccard.RestApplication.main(RestApplication.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [com.cobi.iccard.domain.DeviceConsumeRule]
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321)
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194)
    at org.springframework.core.convert.support.ArrayToObjectConverter.convert(ArrayToObjectConverter.java:68)
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:40)
    ... 47 more

*引发的另一种问题,在JPA的nativeQuery里面 很多的bit对象会被直接转化为 Boolean,这个会让有多种状态值的情况下造成困扰。

解决办法:在数据库连接url配置中加入“&tinyInt1isBit=false”

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cobi_iccard?characterEncoding=utf-8&tinyInt1isBit=false
    原文作者:光影路西法
    原文地址: https://www.jianshu.com/p/fa96a534eda0
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞