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

Use @PathVariable into a Repository [DATAREST-1154]

Open
#1,521 6 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

Daniele Renda opened DATAREST-1154 and commented

I'm using Spring Boot, Spring HATEOAS, Spring Data REST, Hibernate.

All my beans have a Repository in order to expose methods via REST. I would like to use a
@PathVariable in the Repository without create a specific controller.

This is what I would like to do:

@PreAuthorize("isAuthenticated()")
public interface TicketBundleRepository extends JpaRepository<TicketBundle, Long> {

    @Transactional(readOnly = true)
    @RestResource(rel = "ticketBundleNotes", path = "/ticketBundles/{id}/notes")
    @RequestMapping(method = RequestMethod.GET, path = "/ticketBundles/{id}/notes")
    @Query("SELECT n FROM TicketBundle tb JOIN tb.notes n WHERE tb.id=:id ORDER BY n.createdDate DESC, n.id DESC")
    public Page<Note> getNotes(@PathVariable("id") long id, Pageable pageable);

Unfortunately this doesn't work and when I try to call the endpoint I've a http 405 error. I guess the @PathVariable is not supported.

So I turned down to this solution that I don't like so much though:

@Transactional(readOnly = true)
    @Query("SELECT n FROM TicketBundle tb JOIN tb.notes n WHERE tb.id=:id ORDER BY n.createdDate DESC, n.id DESC")
    public Page<Note> getNotes(@Param("id") long id, Pageable pageable);

In short, instead to have a path variable I ask for an url parameter. This works fine but I'm not able to reach my goal to have an enpoint uri like htttp://localhost/api/ticketBundles/{id}/notes.

There is a question opened here: https://stackoverflow.com/questions/46892724/use-pathvariable-into-a-repository-managed-by-spring-data-rest.

I am asking if this could be a new function to add to SDR


Affects: 3.0 GA (Kay)

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.