利用beanutils清除javabean中所有非基本数据类型的属性值:

import com.google.gson.Gson;
import lombok.Data;
import org.apache.commons.beanutils.PropertyUtilsBean;
import java.beans.PropertyDescriptor; @Data
public class RequestParams {
private String name;
private Integer age;
private int d;
private byte bt;
private char c;
private short sd;
private long ld;
private float ft;
private double db;
private boolean tag; /*
* 设置bean中属性值(基本数据类型,请使用封装类)为null
*/
public void clear(){
try {
PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(this);
for (int i = 0; i < descriptors.length; i++) {
String property = descriptors[i].getName();
if (!"class".equals(property)) {
if(!descriptors[i].getPropertyType().isPrimitive()){
propertyUtilsBean.setProperty(this,property,null);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
RequestParams requestParams = new RequestParams();
Gson gson = new Gson();
requestParams.setName("gh");
requestParams.setD(19);
byte b = 101;
requestParams.setBt(b);
requestParams.setC('a');
short s = 20;
requestParams.setSd(s);
requestParams.setLd(19L);
requestParams.setFt(19.1F);
requestParams.setDb(19.9);
requestParams.setTag(false);
requestParams.clear();
System.out.println(gson.toJson(requestParams)); }
}

清除bean中所有非基本数据类型的属性值的更多相关文章

  1. java中循环遍历实体类的属性和数据类型以及属性值

    package com.walkerjava.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTarg ...

  2. java中如何遍历实体类的属性和数据类型以及属性值

      package com.walkerjava.test; import java.lang.reflect.Field; import java.lang.reflect.InvocationTa ...

  3. 【转】java遍历实体类的属性和数据类型以及属性值

    和同学接了个外包的活,由于项目中很多地方要用到poi导出excel,而每次导出都要写很多相同的代码,因为poi的cell.setCellValue();每次设置的都是不同实体bean的属性值,导致代码 ...

  4. Objective-C中变量采用@property的各个属性值的含义

    我们在OC中定义变量,可以自己来定义变量的setter方法来设置变量值,用getter方法来获取变量值.但是当变量数量增多时,还采用手动添加setter/getter方法来操作变量,就会使得程序代码量 ...

  5. java遍历实体类的属性和数据类型以及属性值

    遍历实体类的树形和数据类型一级属性值 /** * 遍历实体类的属性和数据类型以及属性值 * @param model * @throws NoSuchMethodException * @throws ...

  6. C# 随机给一个全部信息都未知的类类型,如何获取该类的类名、属性个数、属性名、属性的数据类型、属性值?

    一.场景假设 假设现在有一个泛型类T的实例对象t,该T类的全部信息都未知. 要求:打印输出实例对象t的类名.属性个数.属性名.属性的数据类型.属性值. 二.解决问题 1.我们根据输出的内容要求定义一个 ...

  7. spring中如何向一个单例bean中注入非单例bean

    看到这个题目相信很多小伙伴都是懵懵的,平时我们的做法大都是下面的操作 @Component public class People{ @Autowired private Man man; } 这里如 ...

  8. java中两个对象间的属性值复制,比较,转为map方法实现

    package com.franson.study.util; import java.lang.reflect.InvocationTargetException; import java.lang ...

  9. jsp中<c:forEach varStatus="status">的属性值问题

    c:forEach varStatus属性 current当前这次迭代的(集合中的)项 index当前这次迭代从 0 开始的迭代索引 count当前这次迭代从 1 开始的迭代计数 first用来表明当 ...

随机推荐

  1. p1501 [国家集训队]Tree II

    传送门 分析 lct板子题 单独维护一下加和乘的情况即可 维护方法和维护翻转差不多 代码 #include<iostream> #include<cstdio> #includ ...

  2. 1.spring.net Look-up Method 查找方法的注入(方法是抽象的需要spring.net注入)

    .为什么需要查找方法的注入 当Object依赖另一个生命周期不同的Object,尤其是当singleton依赖一个non-singleton时,常会遇到不少问题,Lookup Method Injec ...

  3. Windows7 64位 安装mysql

    Windows上安装MySQL还是比较方便的,之前做过一个Windows10上面的安装方法,但是一个同学说自己的电脑是Windows7的,所以我写一个Windows7上的MySQL安装方法. MySQ ...

  4. ASP.NET’s compilation system

    Compilation in ASP.NET applications First, let’s take a moment to revisit compilation in the context ...

  5. Druid详细配置信息

    druid的配置项如下 配置 缺省值 说明 name   配置这个属性的意义在于,如果存在多个数据源,监控的时候 可以通过名字来区分开来.如果没有配置,将会生成一个名字, 格式是:"Data ...

  6. CVPR,ICCV和ECCV,计算机视觉三大顶级盛会

    简介:https://blog.csdn.net/hualitlc/article/details/11099693 近几年ICCV,CVPR,和ECCV论文列表:https://www.xuebuy ...

  7. CodeForces 227E Anniversary (斐波那契的高妙性质+矩阵快速幂)

    There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathemati ...

  8. 20、Semantic-UI之数据验证

    20.1 实现数据验证   在很多前端框架中都提供了数据验证的操作,比如jQuery的验证框架等,但是jQuery的验证框架js文件太多:在使用Semantic-UI框架的时候只需要导入semanti ...

  9. New Year, New Devs: Sharpen your C# Skills

    At the beginning of each new year, many people take on a challenge to learn something new or commit ...

  10. Replication--镜像+复制

    场景:主服务器:Server1从服务器:Server2订阅服务器: Server3镜像DB: RepDB配置:1>配置SERVER3为分发服务器,在Server3上指定发布服务器SERVER1和 ...