Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring data rest mongodb java.lang.IllegalArgumentException: PersistentEntity must not be null

I'm trying to access a mongodb collection via SDR. Working with following versions currently

    <spring.version>4.1.9.RELEASE</spring.version>
    <spring-data-rest>2.4.4.RELEASE</spring-data-rest>
    <spring-data-mongodb>1.8.4.RELEASE</spring-data-mongodb>

My repository looks like

@RepositoryRestResource
@PreAuthorize("hasAuthority('ROLE_USER')")
public interface LinksRepository extends MongoRepository<Link, String> {
Page<Link> findAllByUsefulURLRegex(@Param("regex") String regex, Pageable p);

My model is defined as follows

@Document(collection = "links")
public class Link {
    public Link() {}
    @Id
    private String id;

When I hit http://localhost:9090/api/links I get the following exception

java.lang.IllegalArgumentException: PersistentEntity must not be null!
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.data.rest.webmvc.PersistentEntityResource$Builder.<init>(PersistentEntityResource.java:139)
    at org.springframework.data.rest.webmvc.PersistentEntityResource$Builder.<init>(PersistentEntityResource.java:122)
    at org.springframework.data.rest.webmvc.PersistentEntityResource.build(PersistentEntityResource.java:114)
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.java:102)
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:83)
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:45)
    at org.springframework.data.web.PagedResourcesAssembler.createResource(PagedResourcesAssembler.java:182)
    at org.springframework.data.web.PagedResourcesAssembler.toResource(PagedResourcesAssembler.java:115)
    at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.java:127)
    at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.toResources(AbstractRepositoryRestController.java:88)
    at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.toResource(AbstractRepositoryRestController.java:110)
    at org.springframework.data.rest.webmvc.RepositorySearchController.executeSearch(RepositorySearchController.java:185)
    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:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:775)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:856)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)

Digging a little bit I found MongoMappingContext does not have the class Link as it should (I guess)

enter image description here

I spent few hours trying to figure it out but no luck. I'm not using spring boot and it feels it could be a ObjectMapper problem but I don't know, my domain and setup is simple....any help is greatly appreciated.

Thanks in advance.

like image 366
felipe Avatar asked May 11 '16 23:05

felipe


1 Answers

After an unfair amount of time invested in debugging this to the guts I managed to make it work without any code change on my side with the following configuration:

    <spring.version>4.1.8.RELEASE</spring.version>
    <spring-data-rest>2.3.2.RELEASE</spring-data-rest>
    <spring-data-mongodb>1.8.0.RELEASE</spring-data-mongodb>
    <spring-data-jpa>1.9.0.RELEASE</spring-data-jpa>

It might work with another combination. Just beware.

like image 148
felipe Avatar answered Oct 26 '22 16:10

felipe