Spring 数据 Cassandra Rest Id 必须可分配给 Serializable!:null

Spring data Cassandra Rest Id must be assignable to Serializable!: null

鉴于下面的实体和存储库,我在访问存储库的剩余资源时收到 Id must be assignable to Serializable!: null 错误。

curl -H 'Accept: application/json' http://localhost:8080/properties
{"cause":null,"message":"Id must be assignable to 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 class 上使用 RepositoryRestResource 注释,但收到同样的错误。

@RepositoryRestResource(path = "/properties", collectionResourceRel = "properties")

版本:
Spring 开机:2.0.1.RELEASE
使用 spring-boot-starter-data-cassandra,spring-boot-starter-data-rest 模块

异常堆栈跟踪:

java.lang.IllegalArgumentException: Id must be assignable to Serializable!: null
        at org.springframework.util.Assert.instanceCheckFailed(Assert.java:637)
        at org.springframework.util.Assert.isInstanceOf(Assert.java:537)
        at org.springframework.data.rest.webmvc.support.RepositoryEntityLinks.linkToSingleResource(RepositoryEntityLinks.java:135)
        at org.springframework.data.rest.core.support.DefaultSelfLinkProvider.createSelfLinkFor(DefaultSelfLinkProvider.java:68)
        at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.getSelfLinkFor(PersistentEntityResourceAssembler.java:99)
        at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.java:76)
        at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:55)
        at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.java:110)
        at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.toResources(AbstractRepositoryRestController.java:80)
        at org.springframework.data.rest.webmvc.RepositoryEntityController.getCollectionResource(RepositoryEntityController.java:209)
        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.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)

解决了问题。
如果一个实体 class 有一个复合键,spring 数据 rest 工作 只有当 我有一个专用的 class 用于主键列。
将 class 结构更改为以下,为 spring 数据实体启用了剩余资源。我使用嵌套静态 class 作为键。但它可以很好地成为自己的 public class。

我觉得这个样板应该从开发人员那里移除,取而代之的是 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
    }
}