使用 Spring Data REST 自定义端点 [英] Customize endpoints with Spring Data REST

查看:45
本文介绍了使用 Spring Data REST 自定义端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Spring Boot 1.5.7、Spring Data REST、Hibernate、Spring JPA、Swagger2 的项目.

I've a project with Spring Boot 1.5.7, Spring Data REST, Hibernate, Spring JPA, Swagger2.

我有两个这样的豆子:

    @Entity
public class TicketBundle extends AbstractEntity {
    private static final long serialVersionUID = 404514926837058071L;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Note> notes = new ArrayList<>();

    .....
    }

    @Entity
public class Note extends AbstractEntity {
    private static final long serialVersionUID = -5062313842902549565L;

    @Lob
    private String text;
    ...
    }

我正在通过存储库公开我的方法:

I'm exposing my methods via Repository:

@Transactional
@RepositoryRestResource(excerptProjection = TicketBundleProjection.class)
@PreAuthorize("isAuthenticated()")
public interface TicketBundleRepository extends PagingAndSortingRepository<TicketBundle, Long> {
....
}

因此,在 swagger 中,我看到了我感兴趣的端点,需要从特定的票据包加载笔记集:

so in swagger I see the endpoint in which I'm interested that is needed to load the collection of notes from a specific ticket bundle:

现在,我想覆盖默认的 GET/api/v1/ticketBundles/{id}/notes 并将其替换为我在 TicketBundleRepository 中放入的自定义方法:

Now, I want to override the default GET /api/v1/ticketBundles/{id}/notes and replace that with my custom method I put in TicketBundleRepository:

@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(@Param("id") long id, Pageable pageable);

以这种方式创建查询非常方便,因为我需要使用 Pageable 并返回一个 Page.不幸的是,我现在有两个问题.

It's very convenient create the query in this way because I need to use Pageable and return a Page. Unfortunately I've two problems at this point.

第一个问题该方法映射在 /api/v1/ticketBundles/ticketBundles/{id}/notes<的端点 /api/v1/ticketBundles/search/ticketBundles/{id}/notes 上/代码>

First problem The method is mapped on the endpoint /api/v1/ticketBundles/search/ticketBundles/{id}/notes instad of /api/v1/ticketBundles/ticketBundles/{id}/notes

第二个问题当我从 swagger 调用该方法时,我收到一个 HTTP 404:

Second problem When I call the method from swagger I receive an HTTP 404:

请求似乎是错误的.似乎无法理解路径变量:

The request seems wrong. Seems the path variable is not understood:

curl -X GET --header 'Accept: application/json' 'http://localhost:8080/api/v1/ticketBundles/search/ticketBundles/{id}/notes?id=1'

这是来自服务器的响应:

This is the response from the server:

{
   "timestamp": "2017-10-05T14:00:35.563+0000",
   "status": 404,
   "error": "Not Found",
   "message": "No message available",
   "path": "/api/v1/ticketBundles/search/ticketBundles/%7Bid%7D/notes"
 }

服务器端没有任何错误.

without any error on the server side.

有没有办法覆盖端点 GET/api/v1/ticketBundles/{id}/notes 在不使用自定义控制器的情况下通过 Repository 公开它(使用那个我会失去管理 Pageable 的设施?

Is there a way to override the endpoint GET/api/v1/ticketBundles/{id}/notes exposing it through Repository without using a custom controller (using that I would loose the facilities to manage the Pageable)?

此外,在上面显示的调用中获得 HTTP 404 是我做错了什么?

Furthermore, what am I doing wrong to get a HTTP 404 in the call I shown above?

推荐答案

我相信您使用了不正确的注释.您需要使用 @RestController 注释您的类,并在您的方法上使用 @PathVariable 而不是 @Param.这是一个工作示例,您可能需要根据自己的需要进行定制.

I believe you are using incorrect annotations. You would need to annotate your class with @RestController and use @PathVariable on your method instead of @Param. Here is a working sample, you may want to tailor it according to your needs.

@org.springframework.data.rest.webmvc.RepositoryRestController
@org.springframework.web.bind.annotation.RestController
public interface PersonRepository extends org.springframework.data.repository.PagingAndSortingRepository<Person, Long> {

    @org.springframework.web.bind.annotation.GetMapping(path = "/people/{id}")
    Person findById(@org.springframework.web.bind.annotation.PathVariable("id") Long id);

}

这篇关于使用 Spring Data REST 自定义端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆