NoSuchMethodException on trying to get Nested bean property

半世苍凉 提交于 2019-12-13 03:39:24

问题


I have a Product class:

public class Product {

private ProductClass prodClass;

public ProductClass getProdClass() {
    return prodClass;
}

public void setProdClass(ProductClass prodClass) {
    this.prodClass = prodClass;
}
}

And another one ProductClass ...

public class ProductClass {

private String StbFlag;

public String getStbFlag() {
    return StbFlag;
}

public void setStbFlag(String stbFlag) {
    StbFlag = stbFlag;
}
}

When I try to get the Property using BeanUtils.getNestedProperty as shown below..

public class Test {

public static void main(String Args[]) {

    Product product = new Product();
    ProductClass proClass = new ProductClass();
    proClass.setStbFlag("abcd");
    product.setProdClass(proClass);

    try {
        String value = BeanUtils.getNestedProperty(product, "prodClass.StbFlag");
        System.out.println(value);
    } catch (Exception e) {
        e.printStackTrace();  
    }
}
}

Its throwing the following exception...

java.lang.NoSuchMethodException: Unknown property 'StbFlag' on class 'class ProductClass'
    at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1313)
    at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:762)
    at org.apache.commons.beanutils.BeanUtilsBean.getNestedProperty(BeanUtilsBean.java:715)
    at org.apache.commons.beanutils.BeanUtils.getNestedProperty(BeanUtils.java:354)
    at Test.main(Test.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

What could be the reason? This is just a sample I used to find out the problem. I am actually mapping XML to Java Object and need to keep the name as StbFlag as per the xml tag.

It's working fine when I use STBflag or stbFlag as the variable name. Any workaround for this?


回答1:


BeanUtils expects that your field names start with a lowercase letter, because it adheres to the JavaBean naming conventions.

In Section 8.8 of the JavaBeans spec:

Java programmers are accustomed to having normal identifiers start with lower case letters. Vigorous reviewer input has convinced us that we should follow this same conventional rule for property and event names.

Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example, “FooBah” becomes “fooBah” “Z” becomes “z” “URL” becomes “URL”

We provide a method Introspector.decapitalize which implements this conversion rule

That said, changing your code to this will fix it:

String value = BeanUtils.getNestedProperty(product, "prodClass.stbFlag");

If you are getting the string "StbFlag" from an XML file, I would recommend using the same decapitalize method that BeanUtils uses to convert it to the correct format.

Introspector.decapitalize("StbFlag")

which will return "stbFlag" as a result.




回答2:


public class ProductClass {

private String StbFlag;

public String getStbFlag() {
    return StbFlag;
}

public void setStbFlag(String stbFlag) {
    this.StbFlag = stbFlag;//error here write this get anwser
}
}

write this code anwser is coming



来源:https://stackoverflow.com/questions/15830901/nosuchmethodexception-on-trying-to-get-nested-bean-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!