Spring Data Rest MongoDB Java.lang.IllegalArgumentException: 持久化实体不能为null。

6

我正在尝试通过SDR访问一个MongoDB集合。目前使用以下版本

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

我的仓库看起来像:

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

我的模型定义如下

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

当我访问http://localhost:9090/api/links时,会出现以下异常

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)

挖掘一下,我发现MongoMappingContext没有应该有的Link类(我猜测)。

enter image description here

我花了几个小时来试图解决它,但没有成功。我没有使用spring boot,感觉可能是ObjectMapper的问题,但我不知道,我的域和设置很简单...非常感谢任何帮助。
提前致谢。
1个回答

4

在我花费了不公平的时间对这个问题进行调试之后,我通过以下配置成功地解决了问题,并且没有修改代码:

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

它可能与其他组合一起使用。只要注意。

你能找出是什么导致了错误吗?我正在使用Spring平台的Athens-SR3版本,并且在使用MongoDB和PersistentEntityResourceAssembler时遇到了相同的问题。 - woemler
不好意思,我没有。因为它目前正常运行,所以我停止对此配置进行调整。我相信一旦我升级或者做些改动时,我还需要重新审视这个问题... - felipe
就我个人而言,在使用MockMvc进行测试时,我会遇到这个错误,但在使用Spring Boot运行应用程序时却没有。 - woemler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接