在下面的Entity和Repository中,当我访问存储库的rest资源时,我得到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")
版本: Spring boot:2.0.1.RELEASE 使用spring-boot-starter-data-cassandra,spring-boot-starter-data-rest moduldes
异常Stacktrace:
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)
找出问题所在。 如果实体类具有复合键,则只有当我具有主键列的专用类时,spring数据才会起作用。 将类结构更改为以下,为spring数据实体启用了rest资源。我使用嵌套的静态类作为键。但它本身就是一个公共阶层。
我觉得这个样板应该从开发人员中删除,而春天可以查看分区键列并将其用作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
}
}