日期:2014-05-16  浏览次数:20418 次

JSONUtil.bean2Json()报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;
    }

    /**
     * @return the topCategory
     */
    public Integer getTopCategory() {
        return topCategory;
    }

    /**
     * @param topCategory the topCategory to set
     */
    public void setTopCategory(Integer topCategory) {
        this.topCategory = topCategory;
    }

    /**
     * @param userNum the userNum to set
     */
    public void setUserNum(Integer userNum) {
        this.userNum = userNum;
    }

    /**
     * @return the lastUserNum
     */
    public Integer getLastUserNum() {
        return lastUserNum;
    }

    /**
     * @param lastUserNum the lastUserNum to set
     */
    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 ){
              //--------------中间的代码省略掉