web项目后台判断接口传递参数的对象中的属性是否为空

前言:

抽象出统一判断对象中属性是否为空,可用于检测接口传递的参数中是否有为空的情况,项目解耦。

工具类代码:

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @program: wfanalysisplatform
 * @description: 检查对象中的属性是否为空
 * @author: hangzhang
 * @create: 2020-01-15 10:36
 **/
public class CheckObj {
    /**
     * 检查实体类是否为空
     * */
    public  static boolean checkObjFieldIsNull(Object obj) throws IllegalAccessException {
        boolean flag = false;
        for(Field f : obj.getClass().getDeclaredFields()){
            f.setAccessible(true);
            if(f.get(obj) == null){
                flag = true;
                return flag;
            }
        }
        return flag;
    }
    public static void main(String args[]){
        //测试
        HRDataAnalysis highResolutionReq = new HRDataAnalysis();
        highResolutionReq.setUsername("hz");
        highResolutionReq.setParamlists(new ArrayList<>());
        highResolutionReq.setTbname("");
        highResolutionReq.setTimespan("");
        try {
            boolean b = checkObjFieldIsNull(highResolutionReq);
            System.out.println(b);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

发布了207 篇原创文章 · 获赞 66 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_38025219/article/details/103987911