JSONUtil.bean2Json()报Property 'key' of class has no read method. SKIPPED的问题处理

错误警告信息描述:

net.sf.json.JSONObject.defaultBeanProcessing(JSONObject.java:769) Property ‘handler‘ of class com.vrv.cems.mgr.domain.Manager_$$_javassist_182 has no read method. SKIPPED

问题分析:

JsonUtil.bean2Json(queryHistogramVO,new String[]{}));
将VO对象转换成JSON对象格式
jsonUtil包路径:

queryHistogramVO 对象的属性和方法:

public class HistogramVO {
    private Integer userNum;
    private Integer topCategory;
    private Integer lastUserNum;

    public Integer getCurrentUser() {
        return this.userNum;
    }
    public Integer getTopCategory() {
        return topCategory;
    }
    public void setTopCategory(Integer topCategory) {
        this.topCategory = topCategory;
    }
    public void setUserNum(Integer userNum) {
        this.userNum = userNum;
    }
    public Integer getLastUserNum() {
        return lastUserNum;
    }
    public void setLastUserNum(Integer lastUserNum) {
        this.lastUserNum = lastUserNum;
    }
 }

  肉眼看上去这个类没有任何问题,仔细观察发现 属性"userNum"的get方法为"getCurrentUser()"

详细分析:

1、jsonutil调用类图分析:

  JsonUtil工具类是通过JSONObject.fromObject()方法转换的,查看源码,对fromObject详细分析发现代码:

      //这一句话很关键下面详细讲解
         PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors( bean );
         PropertyFilter jsonPropertyFilter = jsonConfig.getJsonPropertyFilter();
         Class beanClass = bean.getClass();
         for( int i = 0; i < pds.length; i++ ){
            String key = pds[i].getName();
            if( exclusions.contains( key ) ){
               continue;
            }

            if( jsonConfig.isIgnoreTransientFields() && isTransientField( key, beanClass ) ){
               continue;
            }

            Class type = pds[i].getPropertyType();
            //判断如果类的get方法存在则设置属性值
            if( pds[i].getReadMethod() != null ){
              //--------------中间的代码省略掉
               setValue( jsonObject, key, value, type, jsonConfig );
            }else{
               //当get方法不存在报警告错误
               String warning = "Property ‘" + key + "‘ has no read method. SKIPPED";
               fireWarnEvent( warning, jsonConfig );
               log.warn( warning );
            }
         }

  PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors( bean );

  这段代码是获取Bean所有的属性信息并将他封装成 PropertyDescriptor描述类。

  深入 getPropertyDescriptors()分析:

     if (beanClass == null) {
            throw new IllegalArgumentException("No bean class specified");
        }

        // Look up any cached descriptors for this bean class
        PropertyDescriptor[] descriptors = null;
        descriptors =
                (PropertyDescriptor[]) descriptorsCache.get(beanClass);
        if (descriptors != null) {
            return (descriptors);
        }

        // Introspect the bean and cache the generated descriptors
        BeanInfo beanInfo = null;
        try {
            beanInfo = Introspector.getBeanInfo(beanClass);
        } catch (IntrospectionException e) {
            return (new PropertyDescriptor[0]);
        }
        descriptors = beanInfo.getPropertyDescriptors();
        if (descriptors == null) {
            descriptors = new PropertyDescriptor[0];
        }

  上面是关键部分,他是通过java内省机制获取Bean的属性方法,并返回BeanInfo类。

  获取属性的规则:

  1、类中包含 公有get方法如: public String getCurrUser()

  2、类中包含公有的 set方法如:public void setName(String c)

  通过上面的分析,HistogramVO类有setUserNum()方法确没有对应的getUserNum()方法导致报""json.JSONObject - Property ‘userNum‘ has no read method. SKIPPED"警告错误。

  添加getUserNum()方法即解决问题。

JSONUtil.bean2Json()报Property 'key' of class has no read method. SKIPPED的问题处理

原文地址:https://www.cnblogs.com/goloving/p/8195017.html

时间: 2024-10-07 05:30:36

JSONUtil.bean2Json()报Property 'key' of class has no read method. SKIPPED的问题处理的相关文章

CCLuaObjcBridge调Objective-C方法传索引数组报invalid key to &amp;#39;next&amp;#39;错调试

CCLuaObjcBridge是cocos2d-x系列引擎与Objective-C进行交互的"桥梁",老廖的quick-cocos2d-x在其framework进行了简单了封装,封装到了luaoc类中,大体能够看成: luaoc.callStaticMethod = CCLuaObjcBridge.callStaticMethod 函数原型例如以下: --[[ 调用Objective-C中的静态方法 @param string className 类名 @param string me

github添加ssh key报错Key is invalid. Ensure you&#39;ve copied the file correctly

github添加ssh key的时候报错:Key is invalid. Ensure you've copied the file correctly 将秘钥复制粘贴到文本编辑器中,再粘贴复制到 github添加ssh key报错Key is invalid. Ensure you've copied the file correctly

AFNetworking 2.0 编译不过报&quot;Property with &#39;retain (or strong)&#39;attribute must be of object type&quot;问题修复

AFNetworking 2.0 当Deployment Target 低于6.0时,AFURLConnectionOperation.h,AFURLSessionManager.h @property(nonatomic, strong) dispatch_queue_t completionQueue; 由于sdk低于6.0时,dispatch_queue_t  ARC没有托管,出现提示错误 Property with 'retain (or strong)'attribute must b

登录远程Linux服务器:报Host key verification failed错误

远程Linux服务器,报Host key verification failed错误.问题:使用其他电脑登录远程Linux服务器,可以登录,但其中一台电脑登录时报该错误.原因:是因为登录服务器时主机会把它的服务器登录标识证书记录下来,下次登录时会去比对之前的记录,由于系统重装,标识变了导致不能继续登录.解决:在客户端执行命令:ssh-keygen -R 要登录的服务器ip 错误详情和解决: LOVE:~ han$ ssh [email protected] @@@@@@@@@@@@@@@@@@@

python3 load Iris.data数据集出现报错key words: b&#39;Iris-setosa&#39;

通过搜索原因,发现有可能是在对文件读取是编译出现了问题,并且Keyword中提示b'Iris-setosa',而我们的string转float函数中没有字母b,很奇怪.所以尝试将转换函数所有的string前加b.结果发现数据读取正常.下边附上转换函数: def iris_type(s): it = {b'Iris-setosa': 0, b'Iris-versicolor': 1, b'Iris-virginica': 2} return it[s] python3 load Iris.data

sysctl -p &nbsp; 报&quot;unknown key&quot; 错误解决办法

问题描述:在centos 6.5 x64上 # sysctl -p net.ipv4.ip_forward = 1 net.ipv4.conf.default.rp_filter = 0 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 error: "net.bridge.bridge-nf-call-ip6tab

sysctl -P 报&quot;unknown key&quot;错误解决办法

[[email protected] Packages]# sysctl -p net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 error: "net.bridge.bridge-nf-call-

SSH项目练习的时候报错:[applicationContext.xml]: Invocation of init method failed;

这里是控制台的报错信息:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingEx

Jmeter BeanShell 引用变量报错jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Parse error at line 14, column 181 : Error or number too big for integer

如果你通过CSV Data Set Config或者_StringFromFile函数来参数化你的请求,需要特别注意当参数为纯数字时,jmeter会默认将其识别成int型数据,说明jmeter并不是默认以String类型对数据进行读取的:范围-2147483648到2147483647,如果超出这个范围(例如2147483648这个数字):jmeter控制台则会抛出如下异常:jmeter.util.BeanShellInterpreter: Error invoking bsh method: