判空註解spa
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface NullCheckValidate { String message() default "字段不能爲空"; }
枚舉註解code
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface EnumCheckValidate { Class enumClass(); String message() default "類型不匹配"; }
枚舉繼承接口blog
public interface EnumInterface { String getEnumObj(); }
組合條件校驗繼承
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface GroupCheckValidate { /** * 當requst.get(key)=values時,驗證 */ String key(); String value(); boolean isList(); boolean isObject(); String message() default "類型不匹配"; }
實現接口
public static CommonResultResp validatorCheck(Object request) { CommonResultResp result = new CommonResultResp(); Class c = request.getClass(); Field[] fields = c.getDeclaredFields(); for (Field fie : fields) { try { fie.setAccessible(true); /** * 非空校驗 */ NullCheckValidate nullCheckValidate = fie.getAnnotation(NullCheckValidate.class); if (nullCheckValidate != null) { if (StringUtils.isEmpty(fie.get(request))) { result.setResultCode(OmsCommonConsts.RESULE_CODE_FAIL); result.setResultMsg(nullCheckValidate.message()); return result; } continue; } /** * 組合校驗 */ GroupCheckValidate groupCheckValidate = fie.getAnnotation(GroupCheckValidate.class); if (groupCheckValidate != null) { Field value = c.getDeclaredField(groupCheckValidate.key()); value.setAccessible(true); if (value == null || StringUtils.isEmpty(value.get(request))) { value.setAccessible(false); result.setResultCode(OmsCommonConsts.RESULE_CODE_FAIL); result.setResultMsg(groupCheckValidate.message()); return result; } boolean groupV = checkByGroup(fie, request, value.get(request).toString(), groupCheckValidate); value.setAccessible(false); if (!groupV) { result.setResultCode(OmsCommonConsts.RESULE_CODE_FAIL); result.setResultMsg(groupCheckValidate.message()); return result; } } /** * 枚舉校驗 */ EnumCheckValidate enumCheckValidate = fie.getAnnotation(EnumCheckValidate.class); if (enumCheckValidate != null) { boolean enumV = checkByEnum(fie, request, enumCheckValidate.enumClass().getName()); if (!enumV) { result.setResultCode(OmsCommonConsts.RESULE_CODE_FAIL); result.setResultMsg(enumCheckValidate.message()); return result; } continue; } fie.setAccessible(false); } catch (Exception e) { fie.setAccessible(false); e.printStackTrace(); result.setResultCode(OmsCommonConsts.RESULE_CODE_FAIL); result.setResultMsg("參數校驗解析解析異常 code=" + fie.getName()); return result; } } result.setResultCode(OmsCommonConsts.RESULE_CODE_SUCCESS); result.setResultMsg("參數校驗經過"); return result; } private static boolean checkByEnum(Field field, Object request, String className) throws Exception { if (StringUtils.isEmpty(field.get(request))) { return false; } Class<?> onwClass = null; String value = field.get(request).toString(); onwClass = Class.forName(className); Method method = onwClass.getMethod("values"); EnumInterface[] inter = (EnumInterface[]) method.invoke(null); for (EnumInterface enumMessage : inter) { String object = enumMessage.getEnumObj(); if (value.equals(object)) { return true; } } return false; } private static boolean checkByGroup(Field field, Object request, String paramsValue, GroupCheckValidate groupCheckValidate) throws Exception { if (!paramsValue.equals(groupCheckValidate.value())) { return true; } Object o = field.get(request); if (groupCheckValidate.isList()) { if (CollectionUtils.isEmpty((Collection<?>) o)) { return false; } } else { if (o == null) { return false; } } return true; }
須要判斷的類ip
/** * 購買方式 */ @ApiModelProperty(value = "【必填】下單類型:購物車:GWCGM,當即購買:LJGM") @NullCheckValidate private String sourceType; @ApiModelProperty(value = "【當即購買必填】商品列表") @GroupCheckValidate(key = "sourceType", value = "LJGM", isList = true, isObject = false, message = "goodsItem 當即購買必填") private List<OrderGoodsItemDTO> goodsItem; /** * 物流信息 */ @ApiModelProperty(value = "【必填】 配發方式") @EnumCheckValidate(enumClass = OrderShipType.class, message = "shipType 類型不匹配,請參考文檔") private String shipType;
調用文檔
/** * 參數校驗 */ CommonResultResp resp = Utils.validatorCheck(request); if (!resp.isSuccess()) { resp.setResultCode(resp.getResultCode()); resp.setResultMsg(resp.getResultMsg()); return resp; }