一些工具的引入可以是的代码变得更加整洁,同时对于数据的判断会更加的全面
pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.java</groupId> <artifactId>rabbitmq</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--添加fastjson依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.70</version> </dependency> <!--StringUtils--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!--MapUtils--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.2</version> </dependency> <!--CollectionUtils ObjectUtils--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> </dependencies> </project>
代码部分
package com.java; /** * @Description: * @Author: Yourheart * @Create: 2023/5/26 17:29 */ public class Cat { private String name; private Integer age; private String color; public Cat() { } public Cat(String name, Integer age, String color) { this.name = name; this.age = age; this.color = color; } }
package com.java; import org.apache.commons.lang3.ObjectUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.Field; /** * @Description: * @Author: Yourheart * @Create: 2023/5/26 17:44 */ public class ReflectUtil { private static final Logger logger= LoggerFactory.getLogger(ReflectUtil.class); /** * 判断获取的实体类对象是否为空 * @param obj * @return */ public static boolean isEmpty(Object obj) { boolean flag=true; if (ObjectUtils.isNotEmpty(obj)) { Class<?> aClass = obj.getClass(); Field[] fields = aClass.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); Object fieldValue = null; try { fieldValue = field.get(obj); } catch (IllegalAccessException e) { logger.error("获取{},异常:{}",fieldValue,e); } if (ObjectUtils.isNotEmpty(fieldValue)){ flag=false; } } return flag; } else { return flag; } } }
package com.java; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * @Description: * @Author: Yourheart * @Create: 2023/5/26 16:19 */ public class UtilsTests { private final Logger logger= LoggerFactory.getLogger(UtilsTests.class); @Test public void test(){ String string="123"; String string1=null; String string2=""; logger.info(""+StringUtils.isEmpty(string)); logger.info(""+StringUtils.isEmpty(string1)); logger.info(""+StringUtils.isEmpty(string2)); HashMap<String,Object> map=null; HashMap<String,Object> map1=new HashMap<>(16); HashMap<String,Object> map2=new HashMap<>(16); map2.put("aa","1111"); logger.info("MapUtils "+ MapUtils.isEmpty(map)); logger.info("MapUtils "+ MapUtils.isEmpty(map1)); logger.info("MapUtils "+ MapUtils.isEmpty(map2)); List<String> list=null; List<String> list1=new ArrayList<>(10); List<String> list2=new ArrayList<>(10); list2.add("111"); logger.info("CollectionUtils "+ CollectionUtils.isEmpty(list)); logger.info("CollectionUtils "+ CollectionUtils.isEmpty(list1)); logger.info("CollectionUtils "+ CollectionUtils.isEmpty(list2)); Cat cat=new Cat(); Cat cat1=new Cat("兔兔",1,"黑色"); Cat cat2=null; logger.info("实体类对象判断 "+ ReflectUtil.isEmpty(cat)); logger.info("实体类对象判断 "+ ReflectUtil.isEmpty(cat1)); logger.info("实体类对象判断 "+ ReflectUtil.isEmpty(cat2)); } }
效果图
标签:info,常用,java,commons,isEmpty,为空,import,org,logger From: https://www.cnblogs.com/q202105271618/p/17435590.html