英文:
Mapping controller endpoints to Spring JPA layer
问题
我已将您提供的内容翻译如下:
我已编写此请求映射,以通过可选的请求参数ticketType和ticketStatus访问按其id访问票据:
@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返回的方法:
@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);
}
这些端点应该如何在控制器层公开?
目前的端点是:
@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) {
    // 方法体...
}
我应该添加一个新的端点吗?
还是更新控制器以根据查询选择要实现的JPA存储库方法?
@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(tickets.size() == 0){
        return ResponseEntity.ok("No tickets found");
    } else {
        return ResponseEntity.ok(tickets);
    }
}
JPA存储库将进一步扩展以根据许多其他参数(例如ticketType、ticketDescription)进行筛选。我只是提到这一点,因为如果需要检查哪个参数为null,然后选择相关的JPA查询,我的控制器逻辑将变得非常复杂。
英文:
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)
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 ?
Currently the endpoint is:
@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? :
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);
        }
    }
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.
答案1
得分: 0
使用Spring Data JPA规范,可以解决这个映射过滤问题。
可以使用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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论