spring-boot - Spring数据Cassandra Rest Id必须可分配给Serializable!:null
问题描述
在实体和存储库下方给出,Id must be assignable to Serializable!: null
当我访问存储库的其余资源时出现错误。
curl -H '接受:应用程序/json' http://localhost:8080/properties {"cause":null,"message":"Id 必须可分配给 Serializable!:null"}
Groovy 代码
@Component
interface PropertyRepository extends CassandraRepository<Property, String> {
}
@Table("property_v1")
@Canonical
class Property {
@PrimaryKeyColumn(value = "name", type = PARTITIONED)
String name
@PrimaryKeyColumn(value = "environment", type = CLUSTERED)
String environment
@Column("value")
String value
}
我尝试向@Id
主键字段添加注释,但 spring 不允许在同一实体上使用 @Id 和 @PrimaryKeyColumn 注释。
我得到@Table types must not define both @Id and @PrimaryKeyColumn properties
错误。
如何通过休息访问 Spring 数据 Cassandra 实体?
我尝试在 Repository 类上使用 RepositoryRestResource 注释,但收到相同的错误。
@RepositoryRestResource(path = "/properties", collectionResourceRel = "properties")
版本:
Spring Boot:2.0.1.RELEASE
使用 spring-boot-starter-data-cassandra、spring-boot-starter-data-rest 模块
异常堆栈跟踪:
java.lang.IllegalArgumentException:Id 必须可分配给 Serializable!:null 在 org.springframework.util.Assert.instanceCheckFailed(Assert.java:637) 在 org.springframework.util.Assert.isInstanceOf(Assert.java:537) 在 org.springframework.data.rest.webmvc.support.RepositoryEntityLinks.linkToSingleResource(RepositoryEntityLinks.java:135) 在 org.springframework.data.rest.core.support.DefaultSelfLinkProvider.createSelfLinkFor(DefaultSelfLinkProvider.java:68) 在 org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.getSelfLinkFor(PersistentEntityResourceAssembler.java:99) 在 org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.java:76) 在 org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:55) 在 org.springframework.data.rest.webmvc.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.java:110) 在 org.springframework.data.rest.webmvc.AbstractRepositoryRestController.toResources(AbstractRepositoryRestController.java:80) 在 org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(RepositoryEntityController.java:209) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) 在 org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) 在 org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) 在 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) 在 org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) 在 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) 在 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:622) 在 org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 在 org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
解决方案
想通了这个问题。
如果一个实体类有一个复合键,那么只有当我有一个用于主键列的专用类时,spring data rest 才有效。
将类结构更改为以下,为 spring 数据实体启用了休息资源。我使用了一个嵌套的静态类作为键。但它很可能是一个自己的公共课程。
我觉得这个样板应该从开发人员那里删除,而是 spring 可以查看分区键列并将其用作 ID。
@Component
interface PropertyRepository extends CassandraRepository<Property, Property.PropertyKey> {
}
@Table("property_v1")
@Canonical
class Property {
@PrimaryKey
PropertyKey key
@Column("value")
String value
@PrimaryKeyClass
@Canonical
static class PropertyKey implements Serializable {
@PrimaryKeyColumn(value = "name", type = PARTITIONED)
String name
@PrimaryKeyColumn(value = "environment", type = CLUSTERED)
String environment
}
}
推荐阅读
- javascript - 将参数传递给 javascript/nodejs 中的回调
- c - fgets 在第三个字符后切断输入
- javascript - 如何在 Mapbox 的自定义容器中添加图像?
- plot - PSfrag 不适用于 eps 文件的 3D 图
- postgresql - 如何从窗口函数中选择多个值?
- angular - 如何处理 redux 中的成功和失败
- angular - Angular 10 / 如何在具有 2 个类的 1 个文件之间共享 1 个变量 / Angular Material Dialog 组件
- flutter - 为什么只工作一次?
- javascript - 在 fetch API 调用期间访问标头会导致文件数据不可用
- reactjs - 如何在反应传单地图上显示geodjango rest api响应