- 如果需要不同条件验证不同的属性的时候,需要使用
profiles
属性,每个校验注解符中都有。注意:oVal默认是启用所有的profiles,所以在对单独一个profile进行启用的时候,需要先disableAllProfiles
。 - 当使用除了
@NotNull
之外的校验符时,需使用@NotNull
先校验,否则其他校验符不起作用。譬如当使用@ValidateWithMethod
校验符的时候,需要先使用@NotNull
进行校验 - 全部Bean properties代码如下:
@Data
public class NewHouseInputParam {
@NotNull(errorCode = "-10001", message = "orderId不能为空")
private Long orderId;
@NotNull(errorCode = "-1", message = "INPUT订单状态不能为空")
private Integer status;
@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;
@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;
@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;
}
}
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) {
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();
}
}