我正在尝试通过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类(我猜测)。 我花了几个小时来试图解决它,但没有成功。我没有使用spring boot,感觉可能是ObjectMapper的问题,但我不知道,我的域和设置很简单...非常感谢任何帮助。
提前致谢。
Athens-SR3
版本,并且在使用MongoDB和PersistentEntityResourceAssembler
时遇到了相同的问题。 - woemlerMockMvc
进行测试时,我会遇到这个错误,但在使用Spring Boot运行应用程序时却没有。 - woemler