java.lang.NoSuchMethodException:未知属性

时间:2013-10-03 10:09:27

标签: java reflection apache-commons pojo

以下是我的pojo:

package anto.com.poc;
public class VerifyPaymentRO {

private String mihpayid;
private String request_id;
private String bank_ref_num;
private String amt;
private String disc;
private String mode;
private String PG_TYPE;
private String card_no;
private String name_on_card;
private String udf2;
private String addedon;
private String status;
private String unmappedstatus;
private String Merchant_UTR;
private String Settled_At;

public String getMihpayid() {
    return mihpayid;
}
public void setMihpayid(String mihpayid) {
    this.mihpayid = mihpayid;
}
public String getRequest_id() {
    return request_id;
}
public void setRequest_id(String request_id) {
    this.request_id = request_id;
}
public String getBank_ref_num() {
    return bank_ref_num;
}
public void setBank_ref_num(String bank_ref_num) {
    this.bank_ref_num = bank_ref_num;
}
public String getAmt() {
    return amt;
}
public void setAmt(String amt) {
    this.amt = amt;
}
public String getDisc() {
    return disc;
}
public void setDisc(String disc) {
    this.disc = disc;
}
public String getMode() {
    return mode;
}
public void setMode(String mode) {
    this.mode = mode;
}
public String getPG_TYPE() {
    return PG_TYPE;
}
public void setPG_TYPE(String pG_TYPE) {
    PG_TYPE = pG_TYPE;
}
public String getCard_no() {
    return card_no;
}
public void setCard_no(String card_no) {
    this.card_no = card_no;
}
public String getName_on_card() {
    return name_on_card;
}
public void setName_on_card(String name_on_card) {
    this.name_on_card = name_on_card;
}
public String getUdf2() {
    return udf2;
}
public void setUdf2(String udf2) {
    this.udf2 = udf2;
}
public String getAddedon() {
    return addedon;
}
public void setAddedon(String addedon) {
    this.addedon = addedon;
}
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
public String getUnmappedstatus() {
    return unmappedstatus;
}
public void setUnmappedstatus(String unmappedstatus) {
    this.unmappedstatus = unmappedstatus;
}
public String getMerchant_UTR() {
    return Merchant_UTR;
}
public void setMerchant_UTR(String merchant_UTR) {
    Merchant_UTR = merchant_UTR;
}
public String getSettled_At() {
    return Settled_At;
}
public void setSettled_At(String settled_At) {
    Settled_At = settled_At;
}


}

我试图在运行时设置值,如下所示:

static public VerifyPaymentRO convertToVerifyPaymentPOJO(String verifyPaymentInfo) {
        VerifyPaymentRO verifyPaymentRO = new VerifyPaymentRO();
        String[] verifyPaymentComma=null;
        String[] verifyPayment=null;
        String value="";

        verifyPaymentComma = verifyPaymentInfo.trim().split(",");
        for (String verifyPaymentCommaSeparated : verifyPaymentComma) {
            verifyPayment = verifyPaymentCommaSeparated.trim().split("=");
            if(verifyPayment.length==2){
                value=verifyPayment[1];
            }else{
                value="";
            }

             try {

                 if(verifyPayment[0].trim().equals("mihpayid"))
                     PropertyUtils.setProperty(verifyPaymentRO, "mihpayid", value.trim());
                 if(verifyPayment[0].trim().equals("request_id"))
                     PropertyUtils.setProperty(verifyPaymentRO, "request_id", value.trim());
                 if(verifyPayment[0].trim().equals("bank_ref_num"))
                     PropertyUtils.setProperty(verifyPaymentRO, "bank_ref_num", value.trim());
                 if(verifyPayment[0].trim().equals("amt"))
                     PropertyUtils.setProperty(verifyPaymentRO, "amt", value.trim());
                 if(verifyPayment[0].trim().equals("disc"))
                     PropertyUtils.setProperty(verifyPaymentRO, "disc", value.trim());
                 if(verifyPayment[0].trim().equals("mode"))
                     PropertyUtils.setProperty(verifyPaymentRO, "mode", value.trim());
                 if(verifyPayment[0].trim().equals("PG_TYPE"))
                     PropertyUtils.setProperty(verifyPaymentRO, "PG_TYPE", value.trim());
                 if(verifyPayment[0].trim().equals("card_no"))
                     PropertyUtils.setProperty(verifyPaymentRO, "card_no", value.trim());
                 if(verifyPayment[0].trim().equals("name_on_card"))
                     PropertyUtils.setProperty(verifyPaymentRO, "name_on_card", value.trim());
                 if(verifyPayment[0].trim().equals("udf2"))
                     PropertyUtils.setProperty(verifyPaymentRO, "udf2", value.trim());
                 if(verifyPayment[0].trim().equals("addedon"))
                     PropertyUtils.setProperty(verifyPaymentRO, "addedon", value.trim());
                 if(verifyPayment[0].trim().equals("status"))
                     PropertyUtils.setProperty(verifyPaymentRO, "status", value.trim());
                 if(verifyPayment[0].trim().equals("unmappedstatus"))
                     PropertyUtils.setProperty(verifyPaymentRO, "unmappedstatus", value.trim());
                 if(verifyPayment[0].trim().equals("unmappedstatus"))
                     PropertyUtils.setProperty(verifyPaymentRO, "unmappedstatus", value.trim());
                 if(verifyPayment[0].trim().equals("Merchant_UTR"))
                     PropertyUtils.setProperty(verifyPaymentRO, "Merchant_UTR", value.trim());
                 if(verifyPayment[0].trim().equals("Settled_At"))
                     PropertyUtils.setProperty(verifyPaymentRO, "Settled_At", value.trim());

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }



        }

        return verifyPaymentRO;
    }

一切正常但是对于最后两个属性,我得到以下异常:

1. java.lang.NoSuchMethodException: Unknown property 'Merchant_UTR' on class 'class anto.com.poc.VerifyPaymentRO'

2. java.lang.NoSuchMethodException: Unknown property 'Settled_At' on class 'class anto.com.poc.VerifyPaymentRO'

但是这两个字段是可用的,我只得到上面两个字段的上述异常,其余的工作正常。

那么什么可能导致这个问题呢?

感谢

2 个答案:

答案 0 :(得分:4)

使用merchant_UTR和settle_At 在最后两种方法

if(verifyPayment[0].trim().equals("Merchant_UTR"))
                     PropertyUtils.setProperty(verifyPaymentRO, "merchant_UTR", value.trim());
                 if(verifyPayment[0].trim().equals("Settled_At"))
                     PropertyUtils.setProperty(verifyPaymentRO, "settled_At", value.trim());

答案 1 :(得分:3)

正确的属性名称将从低位开始:merchant_UTRsettled_At

顺便说一句,代码中的冗余会造成伤害并且还会使代码非常容易出错(复制粘贴错误)。为什么不把它重写成这样的东西:

List<String> validProperties = Arrays.asList("mihpayid", "request_id", "bank_ref_num", ...);
String property = verifyPayment[0].trim();
if (validProperties.contains(property)) {
    try {
        PropertyUtils.setProperty(verifyPaymentRO, property, value.trim());
    } catch (Exception e) {
        e.printStackTrace();
    }
}