利用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. Fix “Could Not Find This Item” When Deleting in Windows 7

    If you’ve been using Windows for as long as I have, you have probably run into your share of weird e ...

  2. Spirng.net 替换任意方法

    1.首先上客户端代码 static void Main(string[] args)        {            IApplicationContext ctx = ContextRegi ...

  3. 研究wireshark遇到的问题

    说起来有一些惭愧,研究wireshark有一段时间了,但是对源代码的分析却至今没有什么进展... 最初想要研究wireshark是因为我的开题是基于wireshark来做的. 现在有很多抓包工具,wi ...

  4. not in查询不出数据问题

    select ID from SmartCustomer where ID not in (select distinct CustomerID from SmartPromoter where Cu ...

  5. emacs-ide配置

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; CEDET Configuration ;;;;;;;;;; ...

  6. 使用JavaScript获取CSS伪元素属性

    我们可以通过DOM元素的 style 属性获取基本的CSS样式值, 但怎么获取CSS伪元素属性呢? 如下 // 获取 .element:before 的 color 值 var color = win ...

  7. python读取pop3服务器邮件并且下载

    # -*- coding: cp936 -*- import poplib import random import os def getmail(): # 蒋辉文拥有该程序权利 你可以随意使用 em ...

  8. [置顶] bzoj 1036 树的统计Count 点权值模板

    树链剖分 点权型可做模板,链路剖分的思想把点hash到线段树的上,然后可通过n*(log(n)*log(n))的复杂度在树上操作,在线段树上能操作的在链路上都能操作. #include<cstd ...

  9. nginx中级应用-续

    1.server下配置的每个location,都需要有自己的一套代理配置 即要么加入: root  某个目录 要么加入 proxy_pass 某个地址;  proxy_redirect off; # ...

  10. RzToolbutton用法