如果需要不同條件驗證不同的屬性的時候,需要使用profiles
屬性,每個校驗注解符中都有。注意:oVal默認是啟用所有的profiles,所以在對單獨一個profile進行啟用的時候,需要先disableAllProfiles
。
當使用除了@NotNull
之外的校驗符時,需使用@NotNull
先校驗,否則其他校驗符不起作用。譬如當使用@ValidateWithMethod
校驗符的時候,需要先使用@NotNull
進行校驗
全部Bean properties代碼如下:
@Data public class NewHouseInputParam { @NotNull(errorCode = "-10001", message = "orderId不能為空") private Long orderId;// 訂單Id @NotNull(errorCode = "-1", message = "INPUT訂單狀態不能為空") private Integer status; //(1,"錄入報備",""),(2,"錄入到訪",""),(3,"錄入無效",""),(4,"錄入下定",""),(5,"錄入成交",""),(6,"錄入開票",""),(7,"錄入結佣",""), //報備錄入 @NotNull(profiles = {"profile_1"},errorCode = "-1", message = "報備日期不能為空") @ValidateWithMethod(profiles = {"profile_1"},methodName = "isValidDate",parameterType = String.class, errorCode = "-1", message = "報備日期不合法") private String applyDate;// 報備日期 //到訪錄入 @NotNull(profiles = {"profile_2"},errorCode = "-1", message = "到訪日期不能為空") @ValidateWithMethod(profiles = {"profile_2"},methodName = "isValidDate",parameterType = String.class, errorCode = "-1", message = "到訪日期不合法") private String visitDate;// 到訪日期 @NotNull(profiles = {"profile_2"},errorCode = "-1", message = "到訪確認單照片不能為空") private String visitImgKey;// 到訪確認單照片 //下定錄入 @NotNull(profiles = {"profile_4"},errorCode = "-1", message = "下定合同照片不能為空") private String bookImgKey;// 下定合同照片 @NotNull(profiles = {"profile_4"},errorCode = "-1", message = "產品id不能為空") private Long bookProductId;// 產品id @NotNull(profiles = {"profile_4"},errorCode = "-1", message = "下定合同編號不能為空") @MaxLength(value = 20,profiles = {"profile_4"},errorCode = "-1", message = "下定合同編號不合法") private String bookContractCode;// 下定合同編號 @NotNull(profiles = "profile_4",errorCode = "-1", message = "下定金額不能為空") @ValidateWithMethod(profiles = {"profile_4"},methodName = "isValid6Money",parameterType = String.class, errorCode = "-1", message = "下定金額不合法") private String bookPrice;// 下定金額 @NotNull(profiles = "profile_4",errorCode = "-1", message = "購房總價不能為空") @ValidateWithMethod(profiles = {"profile_4"},methodName = "isValid9Money",parameterType = String.class, errorCode = "-1", message = "購房總價不合法") private String bookTotalPrice;// 購房總價 @NotNull(profiles = "profile_4",errorCode = "-1", message = "預計佣金不能為空") @ValidateWithMethod(profiles = {"profile_4"},methodName = "isValid9Money",parameterType = String.class, errorCode = "-1", message = "預計佣金不合法") private String bookPossibleCommission;// 預計佣金 @NotNull(profiles = "profile_4",errorCode = "-1", message = "下定日期不能為空") @ValidateWithMethod(profiles = {"profile_4"},methodName = "isValidDate",parameterType = String.class, errorCode = "-1", message = "下定日期不合法") private String bookDate;// 下定日期 //成交錄入 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "合同照片不能為空") @MinLength(profiles = "profile_5",value = 1,errorCode = "-1",message = "合同照片不能為空") private String dealImgKey;// 合同照片 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "合同總價不能為空") @ValidateWithMethod(profiles = {"profile_5"},methodName = "isValid6Money",parameterType = String.class, errorCode = "-1", message = "合同總價不合法") private String dealContractPrice;// 合同總價 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "產品Id不能為空") private Long dealProductId;// 產品Id @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "合同編號不能為空") @MinLength(profiles = "profile_5",value = 1,errorCode = "-1",message = "合同編號不能為空") private String dealContractCode;// 合同編號 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "應收佣金不能為空") @ValidateWithMethod(profiles = {"profile_5"},methodName = "isValid6Money",parameterType = String.class, errorCode = "-1", message = "應收佣金不合法") private String dealReceivableCommission;// 應收佣金 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "成交日期不能為空") @ValidateWithMethod(profiles = {"profile_5"},methodName = "isValidDate",parameterType = String.class, errorCode = "-1", message = "成交日期不合法") private String dealDate;// 成交日期 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "室不能為空") @Range(profiles = {"profile_5"},min = 0,max = 9,errorCode = "-1", message = "室不合法") private Integer bedroomSum;// 室 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "廳不能為空") @Range(profiles = {"profile_5"},min = 0,max = 9,errorCode = "-1", message = "廳不合法") private Integer livingRoomSum;// 廳 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "衛不能為空") @Range(profiles = {"profile_5"},min = 0,max = 9,errorCode = "-1", message = "衛不合法") private Integer wcSum;// 衛 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "面積不能為空") @ValidateWithMethod(profiles = {"profile_5"},methodName = "isValid4Money",parameterType = String.class, errorCode = "-1", message = "面積不合法") private String spaceArea;// 面積 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "室號不能為空") @MaxLength(profiles = {"profile_5"},value = 10,errorCode = "-1", message = "室號非法") private String room;// 室號 @NotNull(profiles = {"profile_5"},errorCode = "-1", message = "樓棟號不能為空") @MaxLength(profiles = {"profile_5"},value = 10,errorCode = "-1", message = "樓棟號非法") private String building;// 樓棟號 //開票錄入 @NotNull(profiles = {"profile_6"},errorCode = "-1", message = "開票金額不能為空") @ValidateWithMethod(profiles = {"profile_6"},methodName = "isValid6Money",parameterType = String.class, errorCode = "-1", message = "開票金額不合法") private String invoicePrice;// 開票金額 @NotNull(profiles = {"profile_6"},errorCode = "-1", message = "開票日期不能為空") @ValidateWithMethod(profiles = {"profile_6"},methodName = "isValidDate",parameterType = String.class, errorCode = "-1", message = "開票日期不合法") private String invoiceDate;// 開票日期 //結佣錄入 @NotNull(profiles = {"profile_7"},errorCode = "-1", message = "結佣金額不能為空") @ValidateWithMethod(profiles = {"profile_7"},methodName = "isValid6Money",parameterType = String.class, errorCode = "-1", message = "結佣金額不合法") private String commissionPrice;// 結佣金額 @NotNull(profiles = {"profile_7"},errorCode = "-1", message = "結佣日期不能為空") @ValidateWithMethod(profiles = {"profile_7"},methodName = "isValidDate",parameterType = String.class, errorCode = "-1", message = "結佣日期不合法") private String commissionDate;// 結佣日期 //失效錄入 @NotNull(profiles = {"profile_3"},errorCode = "-1", message = "失效原因不能為空") @MaxLength(profiles = {"profile_3"},value = 100,errorCode = "-1", message = "失效原因不合法") private String invalidDesc;// 失效原因 @Override public String toString() { return "NewHouseInputParam{" + "orderId=" + orderId + ", status=" + status + ", applyDate='" + applyDate + '\'' + ", visitDate='" + visitDate + '\'' + ", visitImgKey='" + visitImgKey + '\'' + ", bookImgKey='" + bookImgKey + '\'' + ", bookProductId=" + bookProductId + ", bookContractCode='" + bookContractCode + '\'' + ", bookPrice=" + bookPrice + ", bookTotalPrice=" + bookTotalPrice + ", bookPossibleCommission=" + bookPossibleCommission + ", bookDate='" + bookDate + '\'' + ", dealImgKey='" + dealImgKey + '\'' + ", dealContractPrice=" + dealContractPrice + ", dealProductId=" + dealProductId + ", dealContractCode=" + dealContractCode + ", dealReceivableCommission=" + dealReceivableCommission + ", dealDate='" + dealDate + '\'' + ", bedroomSum=" + bedroomSum + ", livingRoomSum=" + livingRoomSum + ", wcSum=" + wcSum + ", spaceArea='" + spaceArea + '\'' + ", room='" + room + '\'' + ", building='" + building + '\'' + ", invoicePrice='" + invoicePrice + '\'' + ", invoiceDate='" + invoiceDate + '\'' + ", commissionPrice='" + commissionPrice + '\'' + ", commissionDate='" + commissionDate + '\'' + ", invalidDesc='" + invalidDesc + '\'' + '}'; } /** * 驗證日期格式是否合法 * @param date * @return */ public boolean isValidDate(String date){ Date d = DateUtil.stringToDate(date, "YYYY-MM-dd"); return d != null; } /** * 驗證范圍{0.00-9999.99} * @param money * @return */ private boolean isValid4Money(String money){ money = numericalMax2Points(money); if(money != null){ float f = Float.parseFloat(money); if(f >= 0 && f < 10000){ return true; } } return false; } /** * 驗證范圍{0.00-999999.99} * @param money * @return */ private boolean isValid6Money(String money){ money = numericalMax2Points(money); if(money != null){ float f = Float.parseFloat(money); if(f >= 0 && f < 1000000){ return true; } } return false; } /** * 驗證范圍{0.00-999,999,999.99} * @param money * @return */ private boolean isValid9Money(String money){ money = numericalMax2Points(money); if(money != null){ float f = Float.parseFloat(money); if(f >= 0 && f < 1000000000){ return true; } } return false; } /** * 判斷在去除","后是否是數字,並且最多兩位小數 * 正確則返回處理后的money * 否則返回null * @param money * @return */ private String numericalMax2Points(String money){ try { if (money != null) { if (money.indexOf(",") > 0 || money.indexOf(",") > 0) { money = StringUtil.remove(money, ','); money = StringUtil.remove(money, ','); } if (money.matches("-?[0-9]+.?([0-9]{0,2})")) { return money; } } }catch (Exception e){} return null; } }
-
使用oVal代碼,自己封裝的Response:
//驗證參數是否正確 WeixinEntValidatorUtil util = WeixinEntValidatorUtil.getInstance(); util.disableAllProfiles(); util.enableProfile("profile_" + param.getStatus()); res = util.validate(param); if(res.getStatus() != null && res.getStatus() != 0){ return res; }
WeixinEntValidatorUtil代碼如下:
public class WeixinEntValidatorUtil extends ValidatorUtil<Response> { private static final WeixinEntValidatorUtil WEIXIN_ENT_VALIDATOR_UTILS = new WeixinEntValidatorUtil(); public static WeixinEntValidatorUtil getInstance(){return WEIXIN_ENT_VALIDATOR_UTILS;} /** * 失敗 * @param errorCode * @param message * @return */ @Override protected Response transferResponse(String errorCode, String message) { Integer errorCodeInt; try { errorCodeInt = Integer.parseInt(errorCode); }catch (Exception e) { errorCodeInt = -1; } return new Response(errorCodeInt,message); } /** * 成功 * @param o * @return */ @Override protected Response succResponse(Object o) { return new Response(0,""); } public Response validate(Object o){return getInstance()._validate(o);} @Override protected Response _validate(Object o) { return super._validate(o); } }
父類代碼:
public abstract class ValidatorUtil<T> { private static final Validator validator = new Validator(); protected abstract T transferResponse(String errorCode,String message); protected abstract T succResponse(Object o); /** * if the object is null then the util by oval will throw exception * @param o * @return */ protected T _validate (Object o) { List<ConstraintViolation> violations = validator.validate(o); if (violations.size() > 0) { //System.out.println(violations); return transferResponse(violations.get(0).getErrorCode(),violations.get(0).getMessage()); } return succResponse(o); } public void disableProfile(String profileName){ validator.disableProfile(profileName); } public void disableAllProfiles(){ validator.disableAllProfiles(); } public void enableProfile(String profileName){ validator.enableProfile(profileName); } public void enableAllProfiles(){ validator.enableAllProfiles(); } }