尝试获取嵌套bean属性时出现NoSuchMethodException [英] NoSuchMethodException on trying to get Nested bean property

查看:773
本文介绍了尝试获取嵌套bean属性时出现NoSuchMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Product类:

I have a Product class:

public class Product {

private ProductClass prodClass;

public ProductClass getProdClass() {
    return prodClass;
}

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

还有一个ProductClass ...

And another one ProductClass ...

public class ProductClass {

private String StbFlag;

public String getStbFlag() {
    return StbFlag;
}

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

当我尝试使用BeanUtils.getNestedProperty获取属性时,如下所示.

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)

可能是什么原因?这只是我用来找出问题的样本.我实际上是将XML映射到Java对象,并且需要根据xml标签将其名称保持为StbFlag.

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.

当我使用STBflag或stbFlag作为变量名时,它工作正常.有任何解决方法吗?

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

推荐答案

BeanUtils期望您的字段名称以小写字母开头,因为它遵循JavaBean命名约定.

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

JavaBean规范的第8.8节中:

Java程序员习惯于以普通标识符开头 小写字母.审稿人的大力投入使我们深信 对于财产和事件应遵循相同的常规规则 名称.

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.

因此,当我们从中间位置提取属性或事件名称时 现有的Java名称,我们通常将第一个字符转换为小写 案子.但是,为了支持偶尔使用所有大写名称, 我们检查名称的前两个字符是否均为大写 如果是这样,那就别管它了.因此,例如,"FooBah"变为"fooBah" "Z"变成"z""URL"变成"URL"

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"

我们提供了Introspector.decapitalize方法,该方法实现了此转换规则

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");

如果要从XML文件中获取字符串"StbFlag",我建议使用BeanUtils用来将其转换为正确格式的decapitalize方法.

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")

,结果将返回"stbFlag".

这篇关于尝试获取嵌套bean属性时出现NoSuchMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆