spring-projects / spring-projects/spring-data-rest
Repository with @Query methods not correctly [DATAREST-78]
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
Marco Fago opened DATAREST-78 and commented
I have a Spring JPA repository with some methods annotated with @Query and @RestResource (with path value).
@RestResource(path="post")
@Repository
public interface HeatmapPostRepository extends CrudRepository<HeatmapPostEntity, Long>{
@RestResource(path="/content")
@Query("select p from HeatmapPostEntity p,HeatmapAbonnementEntity a where p.abonnement = a and a.name = :name and p.createdAt between :fromDate and :toDate")
List<HeatmapPostEntity> findByNameAndCreatedAtBetweenOrderByIdDesc(@Param("name") String name,@Param("fromDate") Date fromDate,@Param("toDate") Date toDate, Pageable pageable);
@RestResource(path="/count")
@Query("select p.languageCode,count(p) from HeatmapPostEntity p,HeatmapAbonnementEntity a where p.abonnement = a and a.name = :name and p.createdAt between :fromDate and :toDate group by p.languageCode")
Object countByNameAndCreatedAtBetween(@Param("name") String name,@Param("fromDate") Date fromDate,@Param("toDate") Date toDate);
@RestResource(path="/test")
@Query("select p.languageCode,count(p) from HeatmapPostEntity p,HeatmapAbonnementEntity a where p.abonnement = a and a.name = :name group by p.languageCode")
List<Object> countByName(@Param("name") String name);
}
When looking for exported URI I find:
C:\Documents and Settings\Administrator>curl -v "http://localhost:8080/heatmap/post/search"
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /heatmap/post/search HTTP/1.1
> User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Transfer-Encoding: chunked
< Server: Jetty(8.1.8.v20121106)
<
{
"links" : [ {
"rel" : "heatmapPost.findByNameAndCreatedAtBetweenOrderByIdDesc",
"href" : "http://localhost:8080/heatmap/post/search/content"
}, {
"rel" : "heatmapPost.countByName",
"href" : "http://localhost:8080/heatmap/post/search/test"
}, {
"rel" : "heatmapPost.findByIdAndCreatedAtBetween",
"href" : "http://localhost:8080/heatmap/post/search/findByIdAndCreatedAtBetween"
}, {
"rel" : "heatmapPost.countByNameAndCreatedAtBetween",
"href" : "http://localhost:8080/heatmap/post/search/count"
} ],
"content" : [ ]
}* Connection #0 to host localhost left intact
* Closing connection #0
But asking for the listed resource results in a 404(not found) error
C:\Documents and Settings\Administrator>curl -v "http://localhost:8080/heatmap/post/search/test"
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /heatmap/post/search/test HTTP/1.1
> User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Content-Length: 0
< Server: Jetty(8.1.8.v20121106)
<
* Connection #0 to host localhost left intact
* Closing connection #0
C:\Documents and Settings\Administrator>
Calling the resource with the method name still works:
C:\Documents and Settings\Administrator>curl -v "http://localhost:8080/heatmap/post/search/countByName"
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /heatmap/post/search/countByName HTTP/1.1
> User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Transfer-Encoding: chunked
< Server: Jetty(8.1.8.v20121106)
<
{"cause":null,"message":"No query parameter specified for countByName param 'name'"}* Connection #0 to host localhost le
ft intact
* Closing connection #0
Affects: 1.1.0.M1
Referenced from: commits https://github.com/spring-projects/spring-data-rest/commit/7a1151432d50c799e95162261321436978efc97a
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the repository endpoints using the @Query and @RestResource methods shown in the issue, comparing the custom paths with the generated method-name paths. Trace how the /search links are exposed and dispatched, then verify that the custom /content, /count, and /test resources resolve and return their query results instead of 404 responses.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100