Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use @PathVariable into a Repository [DATAREST-1154] #1521

Open
spring-projects-issues opened this issue Oct 26, 2017 · 2 comments
Open

Use @PathVariable into a Repository [DATAREST-1154] #1521

spring-projects-issues opened this issue Oct 26, 2017 · 2 comments
Assignees
Labels

Comments

@spring-projects-issues
Copy link

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)

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

We currently have no plans to add support for this. Writing a controller to front that method invocation is trivial and supporting @RequestMapping on repository query methods creates more questions than it answers

@spring-projects-issues
Copy link
Author

Daniele Renda commented

Thanks for your reply Oliver. Writing a controller is not a big deal but was absolutely fastester use the repository due to facilities (@Query and Pagination). Anyway I understand your point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants