背景
新需求中由于想使用validation的校验注解实现对double类型参数进行条件限制 “不能小于0” ,但发现validation提供的注解都不满足需求场景,未看见支持double校验的注解,@min和 @decimalmin 都未支持,于是就有了自定义一个@doublerange注解实现对double类型的属性进行校验。


一、首先自定义校验规则
1.1 自定义double值规则注解
可以在注解中填入最大值和最小值进行值限制。
/**
* @author yangp
*/
@constraint(validatedby = doublerangevalidator.class)
@target({ elementtype.field, elementtype.method, elementtype.parameter })
public @interface doublerange {
string message() default "值必须在指定范围内";
class<?>[] groups() default {};
class<? extends payload>[] payload() default {};
double min() default double.min_value;
double max() default double.max_value;
}
1.2 自定义double值规则检查器
/**
* @author yangp
*/
public class doublerangevalidator implements constraintvalidator<doublerange, double> {
private double min;
private double max;
@override
public void initialize(doublerange constraintannotation) {
this.min = constraintannotation.min();
this.max = constraintannotation.max();
}
@override
public boolean isvalid(double value, constraintvalidatorcontext context) {
if (value == null) {
return false;
}
return value >= min && value <= max;
}
}
二、类初始化校验
2.1 基类 - 加上自定义double值规则注解
@data
@apimodel(value = "alarmconfigenergyalarmconfigvo", description = "能效告警配置告警信息")
public class alarmconfigenergyalarmconfigvo {
// 添加自定义doublerange自定义校验注解
@apimodelproperty("上上限值")
@doublerange(min = 0, message = "上上限值必须大于0")
private double upuplimitvalue;
@apimodelproperty("上限值")
@doublerange(min = 0, message = "上限值必须大于0")
private double uplimitvalue;
@apimodelproperty("下限值")
@doublerange(min = 0, message = "下限值必须大于0")
private double lolimitvalue;
@apimodelproperty("下下限值")
@doublerange(min = 0, message = "下下限值必须大于0")
private double lololimitvalue;
}
2.2 子类 - 构造器初始化类校验参数(最重要)
这里提供了一个方法,方便对参数进行校验,避免了一个类在多个业务代码层面重复地对属性进行校验,如果需要减少空指针的发生,也可以对需要判空的参数使用@notnull修饰。
- 调用validation的默认校验工厂
- 获取校验器
- 对本类属性值进行证实
- 证实结果为空则参数校验通过,否则校验失败
一行超人版
public class alarmconfigenergyalarmconfigbo extends alarmconfigenergyalarmconfigvo {
public void initcheck() {
bizassert.istrue(validation.builddefaultvalidatorfactory().getvalidator().validate(this).isempty(), "告警配置参数校验失败");
}
}
多行超人版
public class alarmconfigenergyalarmconfigbo extends alarmconfigenergyalarmconfigvo {
public void checkvalue() {
// 获取验证器工厂
validatorfactory validatorfactory = validation.builddefaultvalidatorfactory();
// 获取验证器
validator validator = validatorfactory.getvalidator();
// 进行验证
set<constraintviolation<alarmconfigenergyalarmconfigbo>> validate = validator.validate(this);
// 校验结果
bizassert.istrue(validate.isempty(), "告警配置参数校验失败");
}
}
三、bizassert 神仿小子
陆总的神作 orrz,仿了,分享给大家,可以当作一个工具类使用,避免了多行进行判断。也许未来的你就成为了一行超人。
public class bizassert {
public bizassert() {
}
public static void istrue(boolean expression, string errormsgtemplate, object... params) {
if (!expression) {
throw new businessexception(strutil.format(errormsgtemplate, params));
}
}
public static void notnull(object obj, string errormsgtemplate, object... params) {
if (objectutil.isnull(obj)) {
throw new businessexception(strutil.format(errormsgtemplate, params));
}
}
public static void isnull(object obj, string errormsgtemplate, object... params) {
if (objectutil.isnotnull(obj)) {
throw new businessexception(strutil.format(errormsgtemplate, params));
}
}
public static void notempty(object obj, string errormsgtemplate, object... params) {
if (objectutil.isempty(obj)) {
throw new businessexception(strutil.format(errormsgtemplate, params));
}
}
public static void isempty(object obj, string errormsgtemplate, object... params) {
if (objectutil.isnotempty(obj)) {
throw new businessexception(strutil.format(errormsgtemplate, params));
}
}
public static void iszero(integer obj, string errormsgtemplate, object... params) {
istrue(obj == 0, errormsgtemplate, params);
}
public static void iszero(bigdecimal obj, string errormsgtemplate, object... params) {
istrue(obj != null && obj.compareto(bigdecimal.zero) == 0, errormsgtemplate, params);
}
public static void eq(object obj1, object obj2, string errormsgtemplate, object... params) {
if (!objects.equals(obj1, obj2)) {
throw new businessexception(strutil.format(errormsgtemplate, params));
}
}
public static void ne(object obj1, object obj2, string errormsgtemplate, object... params) {
if (objects.equals(obj1, obj2)) {
throw new businessexception(strutil.format(errormsgtemplate, params));
}
}
public static void tryit(runnable func, string errormsg) {
try {
func.run();
} catch (exception var3) {
exception ex = var3;
throw new businessexception(errormsg, ex);
}
}
public static <t> t tryitandreturn(supplier<t> func) {
try {
return func.get();
} catch (exception var2) {
exception ex = var2;
throw new businessexception("出现错误", ex);
}
}
public static <t> t tryitandreturn(supplier<t> func, string errormsg) {
try {
return func.get();
} catch (exception var3) {
exception ex = var3;
throw new businessexception(errormsg, ex);
}
}
public static void throwexception(string errormsgtemplate, object... params) {
throw new businessexception(strutil.format(errormsgtemplate, params));
}
}
到此这篇关于java使用validation自定义double类型属性校验的文章就介绍到这了,更多相关java validation属性校验内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论