写了一段,供你参考。
public class Test {
class Model {
@
Warning("业务类型不明,请联系管理员")
public String countTypeId;
@
Warning("请选择确切日期进行查询后再进行审核")
public String dealDate;
@
Warning("请选择确切地市进行查询后再进行审核")
public String regionCode;
@
Warning("选择的支付类型有误,请稍后重试")
public String payType;
}
public void Test(){
Model model = new Model();
HashMap<String,String> result = new HashMap<>();
HashMap<String,String> paramMap = new HashMap<>();
Field[] declaredFields = model.getClass().getDeclaredFields();
boolean pass = true;
for (Field field:declaredFields){
if (field.getAnnotation(Warning.class)!=null){
field.setAccessible(true);
try {
Object o = field.get(model);
if (!isVaild(String.valueOf(o))) {
pass = false;
result.put(field.getName(),field.getAnnotation(Warning.class).value());
break;
} else {
paramMap.put(field.getName(),String.valueOf(o).trim());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
if (pass) {
try {
int i = dataRecountService.updateDataPayType(paramMap);
if (i > 0) {
result.put("status", "1");
result.put("msg", "提交成功");
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
result.put("msg", "网络异常,请稍后再尝试。");
}
} else {
}
}
public boolean isVaild(String str){
return StringUtils.isBlank(str) && !"null".equals(str);
}
}
@
Target(FIELD)
@
Retention(RUNTIME)
@
interface Warning{
String value() default "";
}
具体细节可能还要调一下。