1
votes

Given below Entity and Repository, I get Id must be assignable to Serializable!: null error when I access rest resource for repository.

curl -H 'Accept: application/json' http://localhost:8080/properties
{"cause":null,"message":"Id must be assignable to Serializable!: null"}

Groovy code

@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
}

I tried adding @Id annotation to primary key field but spring does not allow @Id and @PrimaryKeyColumn annotations on the same entity.

I get @Table types must not define both @Id and @PrimaryKeyColumn properties error.

How do I access spring data Cassandra entities over rest?

I tried using RepositoryRestResource annotation as well on Repository class but received same error.

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

Versions:
Spring boot: 2.0.1.RELEASE
Uses spring-boot-starter-data-cassandra, spring-boot-starter-data-rest moduldes

Exception 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)

1
Can you please provide the stack trace you get?Oliver Drotbohm
@OliverDrotbohm updated the post with exception tracesuman j

1 Answers

1
votes

Figured out the issue.
If an entity class has a composite key, spring data rest works only if I have a dedicated class for Primary Key columns.
Changing the class structure to below, enabled rest resources for spring data entities. I used a nested static class for key. But it could be very well a public class of its own.

I feel this boiler plate should be removed from developers and instead spring could look into partition key column and use it as 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
    }
}