自定义注解:
@Target(ElementType.FIELD)
@Documented
@Inherited
@JacksonAnnotation
@Retention(RetentionPolicy.RUNTIME)
public @interface BigDecimalAnnotation {
}
使用:
@ApiModelProperty(value = "去年预算")
@ExcelProperty(index = 3)
@BigDecimalAnnotation
private String lastYearBudget;
反射:
/**
* 数据转换和校验
*/
private void dataCheck(XinghuiDetailVo vo) {
// 反射
ReflectionUtils.doWithFields(vo.getClass(), field -> {
// 暴力反射
field.setAccessible(true);
// 判断注解
BigDecimalAnnotation annotation = field.getAnnotation(BigDecimalAnnotation.class);
if (annotation == null) {
return;
}
String value = (String) field.get(vo);
log.debug("value:{}", value);
// 避免空值
if (StringUtils.isBlank(value)) {
return;
}
// 判断字段是否为数字
String str = value.replace("%", "").replace(",", "");
boolean numericzidai = RegularExpression.isNumericzidai(str);
// 设置对应值
if (!numericzidai) {
field.set(vo, null);
} else {
field.set(vo, str);
}
});
}