【问题标题】:Customize endpoints with Spring Data REST使用 Spring Data REST 自定义端点
【发布时间】:2018-03-17 04:54:59
【问题描述】:

我有一个包含 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;
    ...
    }

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

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

因此,我大摇大摆地看到了我感兴趣的端点,它需要从特定票证包中加载笔记集合:

现在,我想覆盖默认的 GET /api/v1/ticketBundles/{id}/notes 并将其替换为我在 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。不幸的是,我现在有两个问题。

第一个问题 该方法映射到端点/api/v1/ticketBundles/search/ticketBundles/{id}/notes instad of /api/v1/ticketBundles/ticketBundles/{id}/notes

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

请求似乎错误。似乎路径变量不理解:

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

这是来自服务器的响应:

{
   "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"
 }

服务器端没有任何错误。

有没有办法覆盖端点GET/api/v1/ticketBundles/{id}/notes 通过Repository 暴露它而不使用自定义控制器(使用它我会失去管理Pageable 的设施)?

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。不幸的是,根据这个jira.spring.io/browse/DATAREST-1154,不可能将@PathVariable 用于RestRepository。我尝试了您的示例,但对我不起作用。我正在使用 Spring Data REST。谢谢!
    猜你喜欢
    • 2018-02-11
    • 2016-03-06
    • 2018-04-18
    • 2016-07-11
    • 2021-07-09
    • 2018-05-13
    • 2015-03-25
    • 2017-11-28
    • 1970-01-01
    相关资源
    最近更新 更多