在接口开发时,最不喜欢的就是接口的入参的为空验证,每个接口的参数都要写比较麻烦,也容易漏掉一些字段,在此提供一份工具类,仅供参考。直接上代码
1、自定义注解:
package com.jy.common; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) /** * 自定义注解 */ public @interface CustomNull { String message() default "{参数不允许为空}";// 注解默认值 }
2、编写父类(用于反射使用的泛型类):
package com.jy.po; /** * 只用于实体类的父类,反射需要使用 */ public class ReflectClazz { }
3、编写工具类(反射实现):
package com.jy.utils; import com.jy.common.CustomNull; import com.jy.po.ReflectClazz; import java.lang.reflect.Field; import java.util.List; import java.util.Objects; /** * 实体属性值为空验证 */ public class CheckUtils { /** * 参数非空验证[反射+自定义注解方式] * return:返回值为null--标识必填项都有值;不为null--返回为空的必填项 */ public static <T extends ReflectClazz> String checkListForNull(List<T> list) { try { for (T item : list) { StringBuffer sb = new StringBuffer(); Class<? extends ReflectClazz> clazz = item.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(CustomNull.class)) { field.setAccessible(true); String value = String.valueOf(field.get(item)); // System.out.println(field.getName()+"="+value); if (StringUtil.isEmpty(value) || Objects.equals("null", value)) { CustomNull CustomNullAnnotation = field.getAnnotation(CustomNull.class); sb.append("【" + CustomNullAnnotation.message() + "】"); } } } if (!StringUtil.isEmpty(sb.toString())) { return sb.toString(); } } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 参数非空验证[反射+自定义注解方式] * return:返回值为null--标识必填项都有值;不为null--返回为空的必填项 */ public static <T extends ReflectClazz> String checkClassForNull(T obj) { StringBuffer sb = new StringBuffer(); try { Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); String value = String.valueOf(field.get(obj)); // System.out.println(field.getName() + "=" + value); if (StringUtil.isEmpty(value) || Objects.equals("null", value)) { CustomNull CustomNullAnnotation = field.getAnnotation(CustomNull.class); sb.append("【" + CustomNullAnnotation.message() + "】"); } if (!StringUtil.isEmpty(sb.toString())) { return sb.toString(); } } } catch (Exception e) { e.printStackTrace(); } return null; } }
4、定义测试实体类
package com.jy.po; import com.jy.common.CustomNull; import lombok.Data; import java.util.List; /** * 人员基本信息 */ @Data public class AgUserInfo extends ReflectClazz { /**主键id*/ private Long id; /**人员姓名*/ @CustomNull(message = "人员姓名不可为空") private String name; /**手机号*/ @CustomNull(message = "手机号不可为空") private String mobile; /**身份证号码*/ @CustomNull(message = "身份证号码不可为空") private String certificateNumber; /**人员执业范围*/ private List<AgUserAppraisalRange> userAppraisalrangeList; /**人员资质证书*/ private List<AgUserAptitude> userAptitudeList; }
package com.jy.vo; import com.jy.common.CustomNull; import com.jy.po.AgOrgInfo; import com.jy.po.AgPowerInfo; import com.jy.po.AgUserInfo; import com.jy.po.ReflectClazz; import lombok.Data; import java.util.List; /** * 组织信息 */ @Data public class OrgnizationVO extends ReflectClazz { /** 组织信息 */ @CustomNull(message = "组织机构信息不可为空") private AgOrgInfo orgInfo; /** 资质信息 */ @CustomNull(message = "资质信息不可为空") private List<AgPowerInfo> powerList; /** 人员信息 */ @CustomNull(message = "人员信息不可为空") private List<AgUserInfo> userList; /** * 标识 */ @CustomNull(message = "标识不可为空") private String interfaceType; }
5、编写demo测试类
package com.jy.common; import com.alibaba.fastjson.JSONObject; import com.jy.constant.CommonConstant; import com.jy.po.*; import com.jy.utils.CheckUtils; import com.jy.utils.DateTimeUtil; import com.jy.utils.StringUtil; import com.jy.vo.OrgnizationVO; import javax.validation.Valid; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.time.LocalDate; import java.util.ArrayList; import java.util.List; import java.util.Objects; /** * 接口测试 */ public class DemoMain { public static void main(String[] args) { OrgnizationVO orgnizationVo = new OrgnizationVO(); // AgOrgInfo orgInfo = initOrgInfo(); // orgnizationVo.setOrgInfo(orgInfo); // // List<AgPowerInfo> powerList = initPowerInfoList(); // orgnizationVo.setPowerList(powerList); List<AgUserInfo> userList = initUerInfoList(); orgnizationVo.setUserList(userList); // 集合对象为空验证 // System.out.println(CheckUtils.checkListForNull(userList)); // 实体对象为空验证 System.out.println(CheckUtils.checkClassForNull(orgnizationVo)); // 初始化机构参数 // System.out.println(orgnizationParamsJson()); } /** 新增机构-参数设计 */ private static String orgnizationParamsJson() { OrgnizationVO orgnizationVo = new OrgnizationVO(); AgOrgInfo orgInfo = initOrgInfo(); orgnizationVo.setOrgInfo(orgInfo); List<AgPowerInfo> powerList = initPowerInfoList(); orgnizationVo.setPowerList(powerList); List<AgUserInfo> userList = initUerInfoList(); orgnizationVo.setUserList(userList); orgnizationVo.setInterfaceType(CommonConstant.INTERFACETYPE_ORGNIZATION); // 参数转JSON字符串 return JSONObject.toJSONString(orgnizationVo); } /** 新增机构-初始化-人员信息-集合 */ private static List<AgUserInfo> initUerInfoList(){ List<AgUserInfo> list = new ArrayList<>(); AgUserInfo userInfo1 = new AgUserInfo(); userInfo1.setName("setName人员1"); userInfo1.setMobile("setMobile12311111111"); userInfo1.setCertificateNumber("setCertificateNumber2374987239847923749827"); List<AgUserAppraisalRange> userAppraisalRangeList1 = new ArrayList<>(); AgUserAppraisalRange userAppraisalRange1 = new AgUserAppraisalRange(); userAppraisalRange1.setAppraisalCategory("setAppraisalCategory人员鉴定类别1"); userAppraisalRange1.setAppraisalItem("setAppraisalItem人员鉴定类型1"); userAppraisalRange1.setAppraisalType("setAppraisalType人员鉴定项目1"); userAppraisalRangeList1.add(userAppraisalRange1); userInfo1.setUserAppraisalrangeList(userAppraisalRangeList1); List<AgUserAptitude> userAptitudeList1 = new ArrayList<>(); AgUserAptitude userAptitude1 = new AgUserAptitude(); userAptitude1.setAptitudeUrl("setAptitudeUrl资质下载地址1"); userAptitude1.setEffectiveStartDate("setEffectiveStartDate有效期开始时间1"); userAptitude1.setEffectiveEndDate("setEffectiveEndDate有效期截止时间1"); userAptitude1.setExecuteNo("setExecuteNo执业证号1"); userAptitude1.setProfession("setProfession职称1"); userAptitudeList1.add(userAptitude1); userInfo1.setUserAptitudeList(userAptitudeList1); list.add(userInfo1); AgUserInfo userInfo2 = new AgUserInfo(); userInfo2.setName("setName人员2"); userInfo2.setMobile("setMobile12322222222"); userInfo2.setCertificateNumber("setCertificateNumber2374987239847923749827"); List<AgUserAppraisalRange> userAppraisalRangeList2 = new ArrayList<>(); AgUserAppraisalRange userAppraisalRange2 = new AgUserAppraisalRange(); userAppraisalRange2.setAppraisalCategory("setAppraisalCategory人员鉴定类别2"); userAppraisalRange2.setAppraisalItem("setAppraisalItem人员鉴定类型2"); userAppraisalRange2.setAppraisalType("setAppraisalType人员鉴定项目2"); userAppraisalRangeList2.add(userAppraisalRange2); userInfo2.setUserAppraisalrangeList(userAppraisalRangeList2); List<AgUserAptitude> userAptitudeList2 = new ArrayList<>(); AgUserAptitude userAptitude2 = new AgUserAptitude(); userAptitude2.setAptitudeUrl("setAptitudeUrl资质下载地址2"); userAptitude2.setEffectiveStartDate("setEffectiveStartDate有效期开始时间2"); userAptitude2.setEffectiveEndDate("setEffectiveEndDate有效期截止时间2"); userAptitude2.setExecuteNo("setExecuteNo执业证号2"); userAptitude2.setProfession("setProfession职称2"); userAptitudeList2.add(userAptitude2); userInfo2.setUserAptitudeList(userAptitudeList2); list.add(userInfo2); return list; } /** 新增机构-初始化-资质信息-集合 */ private static List<AgPowerInfo> initPowerInfoList(){ List<AgPowerInfo> list = new ArrayList<>(); AgPowerInfo powerInfo1 = new AgPowerInfo(); powerInfo1.setName("setName资质1"); powerInfo1.setDescription("setDescription描述1"); powerInfo1.setClassify("setClassify1"); powerInfo1.setImgPath("setImgPathhttp://file/img/aaaa.png"); powerInfo1.setEffectiveStartDate("setEffectiveStartDate"+LocalDate.now()); powerInfo1.setEffectiveEndDate("setNasetEffectiveEndDateme"+LocalDate.now()); powerInfo1.setAptitudeGrade("setAptitudeGrade一级分类1"); powerInfo1.setAptitudeLevel("setAptitudeLevel二级分类1"); powerInfo1.setAptitudeRange("setAptitudeRange三级分类1"); powerInfo1.setRemark("setRemark备注1"); list.add(powerInfo1); AgPowerInfo powerInfo2 = new AgPowerInfo(); powerInfo2.setName("setName资质2"); powerInfo2.setDescription("setDescription描述2"); powerInfo2.setClassify("setClassify2"); powerInfo2.setImgPath("setImgPathhttp://file/img/aaaa.png"); powerInfo2.setEffectiveStartDate("setEffectiveStartDate"+LocalDate.now()); powerInfo2.setEffectiveEndDate("setNasetEffectiveEndDateme"+LocalDate.now()); powerInfo2.setAptitudeGrade("setAptitudeGrade一级分类2"); powerInfo2.setAptitudeLevel("setAptitudeLevel二级分类2"); powerInfo2.setAptitudeRange("setAptitudeRange三级分类2"); powerInfo2.setRemark("setRemark备注2"); list.add(powerInfo2); return list; } /** 新增机构-初始化-机构信息 */ private static AgOrgInfo initOrgInfo() { AgOrgInfo orgInfo = new AgOrgInfo(); orgInfo.setOrgId("setOrgId11111111111"); orgInfo.setOrgName("setOrgName机构1"); orgInfo.setOrgCode("setOrgCode2937492ksjjfssj87"); orgInfo.setRegionCode("setRegionCode0012003"); orgInfo.setLicence("setLicence23423423"); orgInfo.setLegalPerson("setLegalPerson张三"); orgInfo.setPersonLiable("setPersonLiable李四"); orgInfo.setFax("setFax4525452423"); orgInfo.setEmail("setEmail2423@163.com"); orgInfo.setRange("setRange凉快圣诞节是"); orgInfo.setBankAccount("setBankAccount中国银行"); orgInfo.setAccountName("setAccountName公司账户"); orgInfo.setAccountNumber("setAccountNumber928748278279847289742"); orgInfo.setOrgPhone("setOrgPhone13679797979"); orgInfo.setAverageGrade("setAverageGrade123"); orgInfo.setEstablishDate("setEstablishDate"+ LocalDate.now()); orgInfo.setBusinessAcreage("setBusinessAcreage300"); orgInfo.setOrgAppraisalCategory("setOrgAppraisalCategory方式"); orgInfo.setOrgAppraisalType("setOrgAppraisalTypetypeA,typeB,typeC"); orgInfo.setOrgAppraisalItem("setOrgAppraisalItemitemA,itemB,itemC"); orgInfo.setCreditEvaluate("setCreditEvaluate优秀"); orgInfo.setSparePhone("setSparePhone13111111111"); return orgInfo; } }