我正在尝试使用 Micronaut 版本运行 Micronaut 数据 JDBC 示例2.0.1
,我使用 MySQL 数据库作为数据源。
https://github.com/micronaut-projects/micronaut-data/tree/master/examples/example-jdbc-groovy
当我访问http://localhost:8080/owners/
URL 时,它会给出此错误消息。
Internal Server Error: Cannot convert type [class java.lang.Long] with value [1] to target type: class webapp.domain.Owner. Consider defining a TypeConverter bean to handle this case.
这是堆栈跟踪:
8:11:58.387 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 9736ms. Server Running: http://localhost:8080
08:12:10.804 [nioEventLoopGroup-1-2] DEBUG io.micronaut.data.query - Executing Query: SELECT owner_.`id`,owner_.`age`,owner_.`name` FROM `owner` owner_
08:12:11.026 [nioEventLoopGroup-1-2] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: Cannot convert type [class java.lang.Long] with value [1] to target type: class webapp.domain.Owner. Consider defining a TypeConverter bean to handle this case.
io.micronaut.data.exceptions.DataAccessException: Cannot convert type [class java.lang.Long] with value [1] to target type: class webapp.domain.Owner. Consider defining a TypeConverter bean to handle this case.
at io.micronaut.data.runtime.mapper.ResultReader.lambda$convertRequired$0(ResultReader.java:50)
at java.base/java.util.Optional.orElseThrow(Optional.java:408)
at io.micronaut.data.runtime.mapper.ResultReader.convertRequired(ResultReader.java:49)
at io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations$3.tryAdvance(DefaultJdbcRepositoryOperations.java:375)
at java.base/java.util.Spliterator.forEachRemaining(Spliterator.java:326)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations.lambda$findAll$6(DefaultJdbcRepositoryOperations.java:410)
at io.micronaut.transaction.support.AbstractSynchronousTransactionManager.executeRead(AbstractSynchronousTransactionManager.java:155)
at io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations.findAll(DefaultJdbcRepositoryOperations.java:408)
at io.micronaut.data.runtime.intercept.DefaultFindAllInterceptor.intercept(DefaultFindAllInterceptor.java:52)
at io.micronaut.data.runtime.intercept.DefaultFindAllInterceptor.intercept(DefaultFindAllInterceptor.java:36)
at io.micronaut.data.intercept.DataIntroductionAdvice.intercept(DataIntroductionAdvice.java:80)
at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:69)
at io.micronaut.validation.ValidatingInterceptor.intercept(ValidatingInterceptor.java:106)
at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:69)
at webapp.repositories.OwnerRepository$Intercepted.findAll(Unknown Source)
at webapp.repositories.OwnerRepository$findAll.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
at webapp.controllers.OwnerController.all(OwnerController.groovy:20)
at webapp.controllers.$OwnerControllerDefinition$$exec1.invokeInternal(Unknown Source)
如何修复此错误?
谢谢
添加OwnerRepository
代码。
package webapp.repositories
import io.micronaut.data.jdbc.annotation.JdbcRepository
import io.micronaut.data.model.query.builder.sql.Dialect
import io.micronaut.data.repository.CrudRepository
import webapp.domain.Owner
@JdbcRepository(dialect = Dialect.MYSQL)
interface OwnerRepository extends CrudRepository<Owner, Long> {
@Override
List<Owner> findAll()
Optional<Owner> findByName(String name)
}
添加示例回购。https://github.com/sfgroups-k8s/webapp 它会在 MySQL 启动期间失败