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

时间:2019-04-03 16:12:32

标签: spring-boot spring-data spring-data-rest spring-data-cassandra

在实体和存储库下面,访问存储库的其余资源时出现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类上使用RepositoryRestResource批注,但收到相同的错误。

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

版本:
春季启动: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)

1 个答案:

答案 0 :(得分:0)

找出问题所在。
如果实体类具有复合键,则只有在我具有主键列的专用类的情况下,spring数据其余部分才有效。
将类结构更改为以下结构,可以为spring数据实体启用rest资源。我使用嵌套的静态类作为键。但这很可能是一个自己的公共类。

我觉得应该从开发者手中删除此样板,而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
    }
}