将控制器端点映射到Spring JPA层 [英] Mapping controller endpoints to Spring JPA layer

查看:69
本文介绍了将控制器端点映射到Spring JPA层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了此请求映射,以通过带有可选请求参数ticketType&的ID来访问票证. ticketStatus:

I've written this request mapping to access a ticket by it's id with the optional request parameters ticketType & ticketStatus :

@GetMapping(path = "/tickets/{ticketId}")
   @ResponseStatus(value = HttpStatus.OK)
    public ResponseEntity<List<TicketResponse>> getTicketsById(@PathVariable("ticketId") final Long ticketId, @RequestParam("ticketType") final String ticketType, @RequestParam("ticketStatus") final String ticketStatus)

当前,存储库包含用于基于ticketId或ticketState返回的方法:

Currently the repository contains methods for returning based on either the ticketId or the ticketState :

    @Repository
    public interface TicketRepository extends JpaRepository<TicketEntity, Long> {

Stream<TicketEntity> findByTicketIdAndTicketState(@Param("ticketId") Long ticketId);

Stream<TicketEntity> findByTicketIdAndTicketState(@Param("ticketId") Long ticketId, @Param("ticketState") String ticketState);

    }

这些端点应如何在控制器层公开?

How should these endpoints be exposed at the controller layer ?

当前端点为:

@GetMapping(path = "/{ticketId}")
    @ResponseStatus(value = HttpStatus.OK)
    public ResponseEntity<List<TicketResponse>> getTicketsByTicketId(
            @PathVariable("productId") final Long ticketId, @RequestParam(name = "ticketState", required=false) final String ticketState) {

        final List<TicketResponse> ticketsByTicketId = ticketService.getTicketsByTicketId(ticketId);

        if(ticketsByTicketId.size() == 0){
            return ResponseEntity.ok("No tickets found");
        }
        else {
            return ResponseEntity.ok(ticketsByTicketId);
        }

    }

我应该添加一个新的端点吗? :

Should I add a new endpoint? :

或更新控制器以根据查询选择要实现的JPA存储库方法?:

Or update the controller to select which JPA repository method to implement depending on the query ?:

@GetMapping(path = "/{ticketId}")
    @ResponseStatus(value = HttpStatus.OK)
    public ResponseEntity<List<TicketResponse>> getTicketsByTicketId(
            @PathVariable("productId") final Long ticketId, @RequestParam(name = "ticketState", required=false) final String ticketState) {

List<TicketResponse> tickets = null;

if(ticketState == null) {
        tickets = ticketService.getTicketsByTicketId(ticketId);
}
else{
tickets = ticketService.getTicketsByTicketIdAndTicketState(ticketId, ticketState);
}

        if(ticketsByTicketId.size() == 0){
            return ResponseEntity.ok("No tickets found");
        }
        else {
            return ResponseEntity.ok(ticketsByTicketId);
        }

    }

JPA存储库将进一步扩展,以基于更多的参数(例如ticketType,ticketDescription)进行过滤.我只是提到这一点,因为如果需要检查哪个参数为null并选择相关的JPA查询,我的控制器逻辑将变得非常复杂.

The JPA repository will be extended further to filter based on many more parameters such as ticketType, ticketDescription . I just mention this as my controller logic will get very complicated if is required to check which parameter is null and the select the relevant JPA query.

推荐答案

使用Spring Data JPA Specification可以解决此地图过滤问题. 可以使用Criteria API构建复杂的查询,具体取决于过滤参数.

Using Spring Data JPA Specification this map filtering problem can be solved. Complex queries can be build using the Criteria API depends on the filtering parameter.

有关JPA规范的好文章

这篇关于将控制器端点映射到Spring JPA层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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