发送空的搜索规范

时间:2019-08-02 08:39:09

标签: java spring jpa spring-data-jpa spring-data

@GetMapping("find")
    public Page<PaymentTransactionsDTO> getAllBySpecification(
            @And({
                    @Spec(path = "unique_id", spec = LikeIgnoreCase.class),
                    @Spec(path = "merchant_id", spec = In.class),                   
                    @Spec(path = "createdAt", params = "from", spec = GreaterThanOrEqual.class, config="uuuu-MM-dd'T'HH:mm:ss.SSSX"),
                    @Spec(path = "createdAt", params = "to", spec = LessThanOrEqual.class, config="uuuu-MM-dd'T'HH:mm:ss.SSSX")
            }) Specification<PaymentTransactions> specification,
            @SortDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable,
            Authentication authentication) {

        return transactionService.getAllBySpecificationByTerminalId(specification, pageable)
                  .map(g -> PaymentTransactionsDTO.builder()                      
                            .id(g.getId()) 

                            .build()
                    );       
    }

实施:

    @Override
    public Page<PaymentTransactions> getAllBySpecificationByTerminalId(Specification<PaymentTransactions> specification,
            Pageable pageable ) {           
        Specification<PaymentTransactions> finalSpec = specification
                .and(typeIn( ));
        return dao.findAll(finalSpec, pageable);
    }

    private Specification<PaymentTransactions> typeIn( ) {

        List<Integer> terminal_idsdd = new ArrayList<>();
        terminal_idsdd.add(45);
        terminal_idsdd.add(35); 

        return (root, query, cb) -> {
            if (terminal_idsdd != null && !terminal_idsdd.isEmpty()) {
               return root.get(PaymentTransactions_.terminalId).in(terminal_idsdd);
            } else {
               // always-true predicate, means that no filtering would be applied
               return cb.and(); 
            }
        };
    }

设置默认值后,它可以正常工作,但搜索不正确。

@Spec(path = "unique_id", spec = LikeIgnoreCase.class, defaultVal="123"),

默认情况下,UI不会发送任何搜索参数,因此我正在考虑创建类似空的Specification对象之类的东西:

@Override
        public Page<PaymentTransactions> getAllBySpecificationByTerminalId(Specification<PaymentTransactions> specification,
                Pageable pageable ) {

            if (specification == null) {
                Specification<PaymentTransactions> specification = new Specification<PaymentTransactions>();
            }

            Specification<PaymentTransactions> finalSpec = specification
                    .and(typeIn( ));
            return dao.findAll(finalSpec, pageable);
        }

您能指导我如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

我相信您使用的所有http参数都是可选的(默认情况下,所有http参数都是可选的)。因此很明显,当不传递任何参数时,规范将为null。因此,您需要手动处理此空值。我认为您的方法是正确的,但请在下面发布一个简单的建议。希望对您有所帮助。

您可以简单地做到这一点,而不必花费空的对象。

@Override
        public Page<PaymentTransactions> getAllBySpecificationByTerminalId(Specification<PaymentTransactions> specification,
                Pageable pageable ) {

            Specification<PaymentTransactions> finalSpec = null;

            if (specification == null) {
                finalSpec = typeIn();
            } else {

                finalSpec = specification
                                 .and(typeIn( ));
            }
            return dao.findAll(finalSpec, pageable);
        }

其他

您可以将某些参数设为非可选参数,以完全消除规范变为空的机会,因为客户端将被迫传递这些参数。