非字符串字段的自定义实体查找失败



给定以下实体:

@Entity
data class AuthorEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0,
@get: NotNull
@Column(name = "resource_id")
val resourceId: UUID = UUIR.randomUUID()
)

和存储库:

@RepositoryRestResource(collectionResourceRel = "authors", path = "authors")
interface AuthorRepository : PagingAndSortingRepository<AuthorEntity, Long> {
fun findByResourceId(resourceId: UUID): AuthorEntity
}

是否有可能为非字符串字段的Rest Repositories创建自定义实体查找

@Component
class RepositoryRestCustomization : RepositoryRestConfigurer {
override fun configureRepositoryRestConfiguration(config: RepositoryRestConfiguration?) {
config?.withEntityLookup()?.forValueRepository(AuthorRepository::class.java, AuthorEntity::resourceId, AuthorRepository::findByResourceId)
}
}

上述配置导致创建资源时出现java.lang.ClassCastException: java.lang.String cannot be cast to java.util.UUID。然而,当我将resourceId的类型更改为String时,它可以正常工作。


2018年12月24日-编辑:我在一个测试项目中配置了自定义实体查找,结果略有不同。创建和检索所有资源的工作符合预期。尽管如此,访问单个资源会引发上述异常。

详细的堆栈跟踪:

2018-12-24 10:05:58.253错误27829-[nio-9002-exec-5]o.s.d.r.w.RepositoryRestExceptionHandler:java.lang.String不能强制转换为java.util.UUIDjava.lang.ClassCastException:java.lang.String不能转换为java.util.UUID在io.test.configuration.RepositoryRestCustomization$configureRepositoryRestConfiguration$2.ioke(RepositoryRestCustomization.kt:12)~[classes/:na]在io.test.configuration.RepositoryRestCustomization$sam$org_springframework_data_rest_core_config_EntityLookupRegistration_LookupRegister_Lookup$0.loup(RepositoryRestCustomization.kt)~[classes/:na]位于org.springframework.data.rest.core.config.EntityLookupConfiguration$RepositoryEntityLookupEntity(EntityLookupConfiguration.java:214)~[spring-data-rest-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]位于org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappengRepositoryInvokerLambda$invokeFindById$0(UnwrappeingRepositoryInvokerFactory.java:93)~[spring-data-rest-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]在java.util.Optional.flatMap(Optional.java:241)~[na:1.8.0_181]位于org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappengRepositoryInvoker.invokeFindById(UnwrappeingRepositoryInvokerFactory.java:93)~[spring-data-rest-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]位于org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityControlller.java:520)~[spring-data-rest-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]位于org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityControlller.java:333)~[spring-data-rest-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)~[na:1.8.0_181]在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)~[na:1.8.0_181]在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[na:1.8.0_181]位于java.lang.reflect.Method.ioke(Method.java:498)~[na:1.8.0_181]在org.springframework.web.method.support.InvocaleHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]位于org.springframework.web.method.support.InvocaleHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)~[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]位于org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEAE]位于org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandledAdapter.java:800)~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethod Adapter.java:87)~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELENSE]在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]位于org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)~[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELENSE]网址:org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELENSE]位于javax.servlet.httpHttpServlet.service(HttpServlet.java:634)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELENSE]位于javax.servlet.httpHttpServlet.service(HttpServlet.java:741)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)[tomcat-embed-websocket-90.13.jar:9.0.13]网址:org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.springframework.boot.activate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)[spring-boot-atuator-2.1.1.RELEASE.jar:2.1.1.RELEASE]网址:org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequest filter.java:107)[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequest filter.java:107)[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)[spring-web-5..1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequest filter.java:107)[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)[spring-web-5..1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequest filter.java:107)[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117)[spring-boot-actuator-2.1.1.RELEASE.jar:2.1.1.RELEASE]网址:org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106)[spring-boot-actuator-2.1.1.RELEASE.jar:2.1.1.RELEASE]网址:org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequest filter.java:107)[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncoding filter.java:200)[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequest filter.java:107)[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]网址:org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.StandardWrapperValv.invoke(StandardWrapperValve.java:199)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.StandardContextValv.invoke(StandardContextValv.java:96)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.authenticator.AuthenticatorBase.ioke(AuthenticatorBase.java:490)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.StandardHostValv.invoke(StandardHostValv.java:139)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.vvals.ErrorReportValv.invoke(ErrorReportValve.java:92)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.core.StandardEngineValv.invoke(StandardEngineValv.java:74)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.catalina.connecter.CoyoteAdapter.service(Coyotedapter.java:343)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.coyote.AbstractProcessorLight.produce(AbstractProcessorLight.java:66)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)[tomcat-embed-core-9.0.13.jar:9.0.13]网址:org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)[tomcat-embed-core-9.0.13.jar:9.0.13]位于java.util.concurrent.ThreadPoolExecutiator.runWorker(ThreadPoolExecutiator.java:1149)[na:1.8.0_181]位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)[na:1.8.0_181]网址:org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)[tomcat-embed-core-9.0.13.jar:9.0.13]在java.lang.Thread.run(Thread.java:748)[na:1.8.0_181]

您得到该错误是因为JPA不知道谁应该将实体中的UUID类型映射为数据库中的varchar/char类型。JPA实体中使用的属性应该是基元类型,或者是其他实体类型。

最新更新