Jsonutil.bean2json () reported has no Read method. Skipped Problem __json

Source: Internet
Author: User

Problem Analysis: 1, Error warning information description:

2, Decomposition: Call method

Jsonutil.bean2json (Queryhistogramvo,new string[]{}));
    Converts a Vo object to a JSON Object Format jsonutil package path: Queryhistogramvo Object Properties and methods: 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.use
    Rnum = Usernum;
    }/** * @return the Lastusernum */public Integer Getlastusernum () {return lastusernum;
        }/** * @param lastusernum the Lastusernum to set */public void Setlastusernum (Integer lastusernum) { This.lastusernum = lastusernum; }    }

The naked eye looks like there is no problem with this class, and carefully observes that the Get method for the attribute "Usernum" is "Getcurrentuser ()" Detailed analysis:

1, Jsonutil call class Diagram analysis:


The Jsonutil tool class is converted by the Jsonobject.fromobject () method, and the fromobject is analyzed in detail to find the code:

         //This sentence is critical. The following detailed explanation   propertydescriptor[] PDS = PROPERTYUTILS.GETPROPERTYDESCR
         Iptors (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)) {Co
            Ntinue;
Class type = Pds[i].getpropertytype ();
                         //Determine if the class's Get method exists, set the property value if (Pds[i].getreadmethod ()!= null) {
            --------------the middle of the code omitted SetValue (Jsonobject, key, value, type, jsonconfig); }else{///When Get methods do not exist report warning error String warning = "Property '" + key + "' has no Read method.
     Skipped ";          Firewarnevent (Warning, jsonconfig);
            Log.warn (warning); }
         }
propertydescriptor[] pds = propertyutils.getpropertydescriptors (bean);

This code is to get all of the bean's property information and encapsulate it into a PropertyDescriptor description class.
Deep getpropertydescriptors () Analysis:

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];
        }

Introspector.getbeaninfo (beanclass);

The red area above is the key part, and he gets the Bean's property method through the Java introspection mechanism and returns the BeanInfo class.

To get the rule of a property:

1. The class contains a public get method such as: Getcurruser ()

2, the class contains a public-owned set method, such as: publicly void SetName (String c)

Through the above analysis, the HISTOGRAMVO class has the Setusernum () method does not have the corresponding Getusernum () method to cause the "" JSON. Jsonobject-property ' Usernum ' has no Read method. Skipped "Warning error.

Adding the Getusernum () method solves the problem.

Summarize:

The problem is not complicated, but it is understood by the Java introspection mechanism.

Related documents:

Http://java.sun.com/products/javabeans/docs/index.html

Http://blog.csdn.net/hahalzb/archive/2010/10/28/5972421.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.