Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 结合规范并防止NullPointerException和IllegalArgumentException_Java_Spring_Spring Boot_Spring Data Jpa_Spring Data - Fatal编程技术网

Java 结合规范并防止NullPointerException和IllegalArgumentException

Java 结合规范并防止NullPointerException和IllegalArgumentException,java,spring,spring-boot,spring-data-jpa,spring-data,Java,Spring,Spring Boot,Spring Data Jpa,Spring Data,我尝试将8个类似的条件组合成一个简单的查询 我写了一个类似上面的方法 private Specification<CustomerHeaderParameter> buildCustomerHeaderParameterSpecifications(String status, String customerType, Integer segment, String module, String cardType, String processType, String rewardT

我尝试将8个类似的条件组合成一个简单的查询

我写了一个类似上面的方法

private Specification<CustomerHeaderParameter> buildCustomerHeaderParameterSpecifications(String status, String customerType, Integer segment, String module, String cardType, String processType, String rewardType, Integer intervalMin, Integer intervalMax) {

    Specification<CustomerHeaderParameter> result = Specification.where(withStatus(StatusCode.getEnum(status)));
    if (!StringUtils.isEmpty(customerType)) result.and(withCustomerType(CustomerType.getEnum(cardType)));
    if (segment != null) result.and(withSegment(SegmentParameterNumber.valueOf(segment)));
    if (!StringUtils.isEmpty(module)) result.and(withModule(ModuleType.valueOf(module)));
    if (!StringUtils.isEmpty(cardType)) result.and(withCardType(CardType.getEnum(cardType)));
    if (!StringUtils.isEmpty(processType)) result.and(withProcessType(ProcessType.valueOf(processType)));
    if (!StringUtils.isEmpty(rewardType)) result.and(withRewardType(RewardType.valueOf(rewardType)));
    if (intervalMin != null && intervalMax != null) result.and(inInterval(intervalMin, intervalMax));

    return result;
}
我的静态方法与

static Specification<CustomerHeaderParameter> withStatus(StatusCode status) {
    return (param, cq, cb) -> cb.equal(param.get(STATUS_CODE), status);
}
status静态规范(StatusCode状态){
返回(param,cq,cb)->cb.equal(param.get(STATUS_CODE),STATUS);
}
如果方法参数为null或与枚举中的值不同,如何防止异常


或一般性问题;组合规范的最佳实践是什么?

我通过自定义规范类解决了这个问题

public class CustomerHeaderParameterSpecification implements Specification<CustomerHeaderParameter> {

    private StatusCode status;
    private CustomerType customerType;
    private SegmentParmNumber segment;
    private ModuleType module;
    private CardType cardType;
    private ProcessType processType;
    private RewardType rewardType;
    private Integer min, max;


    public CustomerHeaderParameterSpecification(StatusCode status, CustomerType customerType, SegmentParmNumber segment, ModuleType module, CardType cardType, ProcessType processType, RewardType rewardType, Integer min, Integer max) {
          this.status = status;
          this.customerType = customerType;
          this.segment = segment;
          this.module = module;
          this.cardType = cardType;
          this.processType = processType;
          this.rewardType = rewardType;
          this.min = min;
          this.max = max;
    }

    @Override
    public Predicate toPredicate(Root<CustomerHeaderParameter> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

        List<Predicate> predicates = new ArrayList<>();
        if (this.status != null) predicates.add(criteriaBuilder.equal(root.get(STATUS_CODE), status));
        if (this.customerType != null) predicates.add(criteriaBuilder.equal(root.get(CUSTOMER_TYPE), customerType));
        if (this.segment != null) predicates.add(criteriaBuilder.equal(root.get(SEGMENT_PARM_NUMBER), segment));
        if (this.module != null) predicates.add(criteriaBuilder.equal(root.get(MODULE_TYPE), module));
        if (this.cardType != null) predicates.add(criteriaBuilder.equal(root.get(CARD_TYPE), cardType));
        if (this.processType != null) predicates.add(criteriaBuilder.equal(root.get(PROCESS_TYPE), processType));
        if (this.rewardType != null) predicates.add(criteriaBuilder.equal(root.get(REWARD_TYPE), rewardType));
        if (this.min != null && this.max != null) predicates.add(criteriaBuilder.between(root.get(PARAMETER_NUM), min, max));

        return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
    }

}
公共类CustomerHeaderParameterSpecification实现了该规范{
私有状态码状态;
私有CustomerType CustomerType;
私有段parmnumber段;
私有模块类型模块;
私人卡式卡式;
私有进程类型进程类型;
私有报酬型报酬型;
私有整数最小值,最大值;
公共CustomerHeaderParameterSpecification(状态代码状态、CustomerType CustomerType、SegmentParmNumber段、ModuleType模块、CardType CardType、ProcessType ProcessType、RewardType RewardType、最小整数、最大整数){
这个状态=状态;
this.customerType=customerType;
本段=段;
this.module=模块;
this.cardType=cardType;
this.processType=processType;
this.rewardType=rewardType;
this.min=min;
this.max=max;
}
@凌驾
公共谓词toPredicate(根根、CriteriaQuery CriteriaQuery、CriteriaBuilder CriteriaBuilder){
列表谓词=新的ArrayList();
if(this.status!=null)predicates.add(criteriaBuilder.equal(root.get(status_代码),status));
if(this.customerType!=null)谓词.add(criteriaBuilder.equal(root.get(CUSTOMER_TYPE),customerType));
if(this.segment!=null)谓词.add(criteriaBuilder.equal(root.get(segment\u PARM\u NUMBER),segment));
if(this.module!=null)predicates.add(criteriaBuilder.equal(root.get(module_类型),module));
if(this.cardType!=null)谓词.add(criteriaBuilder.equal(root.get(CARD_-TYPE),cardType));
if(this.processType!=null)predicates.add(criteriaBuilder.equal(root.get(PROCESS_TYPE),processType));
if(this.rewardType!=null)谓词.add(criteriaBuilder.equal(root.get(REWARD_类型),rewardType));
if(this.min!=null&&this.max!=null)谓词.add(criteriaBuilder.between(root.get(PARAMETER_NUM),min,max));
返回criteriaBuilder.and(谓词.toArray(新谓词[0]);
}
}

那么,如果状态为空,您想做什么?如果状态不是您的某个枚举,您想做什么?@realpoinsistİf status为null或不是我的某个枚举,我不想将其添加到我的规范中。然后,它应该像where 1=1,然后我的'and'子句将出现在后面。然后,添加一个
if
语句并准确地执行该操作。@realpoint在方法'buildCustomerHeaderParameterSpecifications'中,如果我在规范上添加一个空检查。where(with status(status code.getEnum(status));当status为null时,将不调用where语句。这是个问题,不是吗?
public class CustomerHeaderParameterSpecification implements Specification<CustomerHeaderParameter> {

    private StatusCode status;
    private CustomerType customerType;
    private SegmentParmNumber segment;
    private ModuleType module;
    private CardType cardType;
    private ProcessType processType;
    private RewardType rewardType;
    private Integer min, max;


    public CustomerHeaderParameterSpecification(StatusCode status, CustomerType customerType, SegmentParmNumber segment, ModuleType module, CardType cardType, ProcessType processType, RewardType rewardType, Integer min, Integer max) {
          this.status = status;
          this.customerType = customerType;
          this.segment = segment;
          this.module = module;
          this.cardType = cardType;
          this.processType = processType;
          this.rewardType = rewardType;
          this.min = min;
          this.max = max;
    }

    @Override
    public Predicate toPredicate(Root<CustomerHeaderParameter> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

        List<Predicate> predicates = new ArrayList<>();
        if (this.status != null) predicates.add(criteriaBuilder.equal(root.get(STATUS_CODE), status));
        if (this.customerType != null) predicates.add(criteriaBuilder.equal(root.get(CUSTOMER_TYPE), customerType));
        if (this.segment != null) predicates.add(criteriaBuilder.equal(root.get(SEGMENT_PARM_NUMBER), segment));
        if (this.module != null) predicates.add(criteriaBuilder.equal(root.get(MODULE_TYPE), module));
        if (this.cardType != null) predicates.add(criteriaBuilder.equal(root.get(CARD_TYPE), cardType));
        if (this.processType != null) predicates.add(criteriaBuilder.equal(root.get(PROCESS_TYPE), processType));
        if (this.rewardType != null) predicates.add(criteriaBuilder.equal(root.get(REWARD_TYPE), rewardType));
        if (this.min != null && this.max != null) predicates.add(criteriaBuilder.between(root.get(PARAMETER_NUM), min, max));

        return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
    }

}