常用的参数验证和操作

1、验证容器是否为空

CollectionUtils.isNotEmpty(iList)

2、String类型是否为空

StringUtils.isNotEmpty(rootSuit)

StringUtils.hasText(fromDate)

3、idString是否符合UUID的格式

CommonHelper.checkUuidPattern(rootSuit)

4、检验日期类型是否符合要求

public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        format.setLenient(false);
        try {
            format.parse(str);
        } catch (java.text.ParseException e) {

            throw new LifeCircleException(HttpStatus.INTERNAL_SERVER_ERROR,
                    LifeCircleErrorMessageMapper.DateFormatFail);
        }
        return convertSuccess;
    }

5、对入参limit格式如(3,5)这样的校验,采用ParamCheckUtil.checkLimit(limit),也可以通过这个方式来获取limit的前一个参数和后一个参数

//limit校验
        ParamCheckUtil.checkLimit(limit);

6、对includes变量的操作,对includes字符串按逗号进行分开
IncludesConstant.getValidIncludes(includes);

 public static List getValidIncludes(String includes){
        if(StringUtils.isEmpty(includes)){
            return new ArrayList();
        }
        Set set = new HashSet(Arrays.asList(includes.split(",")));
        List includesList = getIncludesList();
        for(String include : set){
            if(!includesList.contains(include.trim())){
                throw new LifeCircleException(HttpStatus.INTERNAL_SERVER_ERROR,
                        LifeCircleErrorMessageMapper.IncludesParamError.getCode(),
                        "includes中的:" + include + ",不在规定范围内");
            }
        }    
        return new ArrayList(set);
    }

7、对resType的方式,用IndexSourceType类来取

IndexSourceType.AssetType.getName();//取asstes的名字asstes

你可能感兴趣的:(常用的参数验证和操作)