将@PathVariable用于由Spring Data REST管理的存储库

时间:2017-10-23 15:08:49

标签: java spring spring-mvc spring-boot spring-data-rest

我正在使用Spring Boot,Spring HATEOAS,Spring Data REST,Hibernate。

我的所有bean都有一个Repository,以便通过REST公开方法。 我想在Repository中使用@PathVariable而不创建特定的控制器。

这就是我想做的事情:

@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);

不幸的是,这不起作用,当我尝试调用端点时,我遇到了一个http 405错误。我想不支持@PathVariable。

所以我拒绝了这个我不太喜欢的解决方案:

    @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);

简而言之,我想要一个url参数来代替路径变量。这很好但我无法达到我的目标,即拥有像htttp://localhost/api/ticketBundles/{id}/notes这样的uri uri。

是否有可能在Repository中使用@PathVariable?

0 个答案:

没有答案