gpt4 book ai didi

java - 尝试获取嵌套 bean 属性时出现 NoSuchMethodException

转载 作者:行者123 更新时间:2023-12-02 07:08:28 25 4
gpt4 key购买 nike

我有一个产品类别:

public class Product {

private ProductClass prodClass;

public ProductClass getProdClass() {
return prodClass;
}

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

还有一个 ProductClass ...

public class ProductClass {

private String StbFlag;

public String getStbFlag() {
return StbFlag;
}

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

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

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();
}
}
}

它抛出以下异常...

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。

当我使用 STBflag 或 stbFlag 作为变量名时,它工作正常。有什么解决办法吗?

最佳答案

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

JavaBeans spec 第 8.8 节中:

Java programmers are accustomed to having normal identifiers start withlower case letters. Vigorous reviewer input has convinced us that weshould follow this same conventional rule for property and eventnames.

Thus when we extract a property or event name from the middle of anexisting Java name, we normally convert the first character to lowercase. However to support the occasional use of all upper-case names,we check if the first two characters of the name are both upper caseand 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

也就是说,将您的代码更改为此将修复它:

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

如果您从 XML 文件获取字符串 "StbFlag",我建议您使用与 BeanUtils 相同的 decapitalize 方法将其转换为正确的格式。

Introspector.decapitalize("StbFlag")

它将返回“stbFlag”作为结果。

关于java - 尝试获取嵌套 bean 属性时出现 NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15830901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com