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

标签 java spring spring-boot kotlin spring-data-rest

给定以下实体:

@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.UUID

java.lang.ClassCastException:无法将java.lang.String强制转换为java.util.UUID
在io.test.configuration.RepositoryRestCustomization $ configureRepositoryRestConfiguration $ 2.invoke(RepositoryRestCustomization.kt:12)〜[classes /:na]
在io.test.configuration.RepositoryRestCustomization $ sam $ org_springframework_data_rest_core_config_EntityLookupRegistrar_LookupRegistrar_Lookup $ 0.lookup(RepositoryRestCustomization.kt)〜[classes /:na]
在org.springframework.data.rest.core.config.EntityLookupConfiguration $ RepositoriesEntityLookup.lookupEntity(EntityLookupConfiguration.java:214)〜[spring-data-rest-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]
在org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory $ UnwrappingRepositoryInvoker.lambda $ invokeFindById $ 0(UnwrappingRepositoryInvokerFactory.java:93)〜[spring-data-rest-core-3.1.3.RELEASE.jar:3.1.3。释放]
在java.util.Optional.flatMap(Optional.java:241)〜[na:1.8.0_181]
在org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory $ UnwrappingRepositoryInvoker.invokeFindById(UnwrappingRepositoryInvokerFactory.java:93)〜[spring-data-rest-core-3.1.3.RELEASE.jar:3.1.3.RELEASE]
在org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:520)〜[spring-data-rest-webmvc-3.1.3.RELEASE.jar:3.1.3.RELEASE]
在org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.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.invoke(Method.java:498)〜[na:1.8.0_181]
在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)〜[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
在org.springframework.web.method.support.InvocableHandlerMethod.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.RELEASE]
在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)〜[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)〜[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
在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.RELEASE]
在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)[spring-webmvc-5.1.3.RELEASE.jar:5.1.3.RELEASE]
在javax.servlet.http.HttpServlet.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.RELEASE]
在javax.servlet.http.HttpServlet.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-9.0.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.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90)上[spring-boot-actuator-2.1.1.RELEASE.jar:2.1.1.RELEASE]
在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.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(OncePerRequestFilter.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(OncePerRequestFilter.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(OncePerRequestFilter.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(OncePerRequestFilter.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(CharacterEncodingFilter.java:200)[spring-web-5.1.3.RELEASE.jar:5.1.3.RELEASE]
在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.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.StandardWrapperValve.invoke(StandardWrapperValve.java:199)[tomcat-embed-core-9.0.13.jar:9.0.13]
在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)[tomcat-embed-core-9.0.13.jar:9.0.13]
在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)[tomcat-embed-core-9.0.13.jar:9.0.13]
在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)[tomcat-embed-core-9.0.13.jar:9.0.13]
在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)[tomcat-embed-core-9.0.13.jar:9.0.13]
在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)[tomcat-embed-core-9.0.13.jar:9.0.13]
在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.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.process(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.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.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不知道谁将db中的UUID类型映射到您实体中的varchar/char类型。 JPA实体中使用的属性应为原始类型或其他实体类型。

关于java - 非字符串字段的自定义实体查找失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53860105/

相关文章:

java - Spring Boot 使用 docker 返回静态资源 404

java - 如何在 Java 中声明一个 "infinite"二维数组?

java - Java 中 Final 字段和 volatile 字段之间内存模型语义的差异

java - 如何在java中检查文件是否受密码保护/加密

java - NullPointerException 创建自己的 JSON 响应对象

Azure Web 部署失败 - 给定名称的网站已存在

java - Angular2 在使用 XMLHttpRequest 发帖时正在刷新页面

spring - 使用 Spring Boot 外部 log4.properties 文件

java - Spring JPA更新并返回更新后的数据

mysql - SQL异常 : Access denied in Spring Boot