spring-projects / spring-projects/spring-data-rest

Allow GeoPage as return type [DATAREST-439]

Open
#819 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

in: repository type: enhancement
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Thomas Darimont opened DATAREST-439 and commented

Currently we don't support GeoPage as a result type for finder- methods with Spring Data Rest Repositories.

I stumbled upon this while writing a simple Web Front-end for the starbucks spring-data-examples app.
If I change the return type of StoreRepository.findByAddressLocationNear from Page<Store> to GeoPage<Store> and issue a query via curl, I get the following error message:

{"cause":null,"message":"PersistentEntity must not be null!"}
Page<Store> findByAddressLocationNear(//
			@Param("location") Point location, @Param("distance") Distance distance, Pageable pageable);

Steps to reproduce:

  1. Run the starbucks example app
  2. Perfom a get request to get stores near timesquare via curl
curl http://localhost:8080/stores/search/findByAddressLocationNear\?location\=40.740337%2C-73.995146\&distance\=0.5miles\&page\=0\&size\=100

On the server one should see the following stacktrace:

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:155)
	at org.springframework.data.rest.webmvc.PersistentEntityResource$Builder.<init>(PersistentEntityResource.java:137)
	at org.springframework.data.rest.webmvc.PersistentEntityResource.build(PersistentEntityResource.java:129)
	at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.java:105)
	at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:84)
	at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:45)
	at org.springframework.data.web.PagedResourcesAssembler.createResource(PagedResourcesAssembler.java:137)
	at org.springframework.data.web.PagedResourcesAssembler.toResource(PagedResourcesAssembler.java:96)
	at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.java:228)
	at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.resultToResources(AbstractRepositoryRestController.java:215)
	at org.springframework.data.rest.webmvc.RepositorySearchController.executeQueryMethod(RepositorySearchController.java:291)
	at org.springframework.data.rest.webmvc.RepositorySearchController.executeSearch(RepositorySearchController.java:175)
	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:483)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
	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.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
	at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:744)

The problem is that a GeoPage wraps the actual result within a GeoResult which crashes in org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler#wrap since source.getClass() is now a GeoResult instead of the actual entity type (Store in this case).

A quick hack for testing was to change the wrap(...) method to handle the special case of a GeoResult, which leads to the expected result.

	private Builder wrap(Object instance, Object source) {

		Object element = source;
		if(element instanceof GeoResult){
			element = ((GeoResult)source).getContent();
		}

		PersistentEntity<?, ?> entity = repositories.getPersistentEntity(element.getClass());

		return PersistentEntityResource.build(instance, entity).//
				withEmbedded(getEmbeddedResources(element)).//
				withLink(getSelfLinkFor(element));
	}

Yields:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/stores/search/findByAddressLocationNear?location=40.740337%2C-73.995146&distance=0.5miles&page=0&size=100{&sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "geoResults" : [ {
      "content" : {
        "name" : "22nd St. & Sixth Ave.",
        "address" : {
          "street" : "684 Avenue of the Americas",
          "city" : "New York",
          "zip" : "10010-5110",
          "location" : {
            "x" : -73.993237,
            "y" : 40.741844
          }
        }
      },
      "distance" : {
        "value" : 0.14448373542533344,
        "metric" : "MILES"
      },
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/stores/54b63d2dd4c669674288254a"
        }
      }
    },
...
} ]
  },
  "page" : {
    "size" : 100,
    "totalElements" : 19,
    "totalPages" : 1,
    "number" : 0
  }
}

Affects: 2.3 M1 (Fowler)

Reference URL: https://github.com/spring-projects/spring-data-examples/pull/47

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.