java - 将 xml 解析为 java 对象时没有单字符串构造函数/工厂方法错误

标签 java json xml jackson fasterxml

我正在尝试将 xml 响应转换为 java 对象,但解析时出现错误。我尝试了所有选项但没有成功。 xml 可以有多个标签。 该错误基本上是在将 xml 标签转换为数组对象时出现的。 我的类(class)是:

账户摘要回复:

public class AccountSummaryReply {
    @JacksonXmlProperty(localName = "AccountSubType")
    private String AccountSubType;

    @JacksonXmlProperty(localName = "WorkingBalance")
    private String WorkingBalance;

    @JacksonXmlProperty(localName = "AccountStatus")
    private String AccountStatus;

    @JacksonXmlProperty(localName = "AccountNumber")
    private String AccountNumber;

    @JacksonXmlProperty(localName = "BranchCode")
    private String BranchCode;

    @JacksonXmlProperty(localName = "AccountName")
    private String AccountName;

    @JacksonXmlProperty(localName = "OpenActualBalance")
    private String OpenActualBalance;

    @JacksonXmlProperty(localName = "Currency")
    private String Currency;

    @JacksonXmlProperty(localName = "IBAN")
    private String IBAN;

    @JacksonXmlProperty(localName = "AccountType")
    private String AccountType;

    public String getAccountSubType ()
    {
        return AccountSubType;
    }

    public void setAccountSubType (String AccountSubType)
    {
        this.AccountSubType = AccountSubType;
    }

    public String getWorkingBalance ()
    {
        return WorkingBalance;
    }

    public void setWorkingBalance (String WorkingBalance)
    {
        this.WorkingBalance = WorkingBalance;
    }

    public String getAccountStatus ()
    {
        return AccountStatus;
    }

    public void setAccountStatus (String AccountStatus)
    {
        this.AccountStatus = AccountStatus;
    }

    public String getAccountNumber ()
    {
        return AccountNumber;
    }

    public void setAccountNumber (String AccountNumber)
    {
        this.AccountNumber = AccountNumber;
    }

    public String getBranchCode ()
    {
        return BranchCode;
    }

    public void setBranchCode (String BranchCode)
    {
        this.BranchCode = BranchCode;
    }

    public String getAccountName ()
    {
        return AccountName;
    }

    public void setAccountName (String AccountName)
    {
        this.AccountName = AccountName;
    }

    public String getOpenActualBalance ()
    {
        return OpenActualBalance;
    }

    public void setOpenActualBalance (String OpenActualBalance)
    {
        this.OpenActualBalance = OpenActualBalance;
    }

    public String getCurrency ()
    {
        return Currency;
    }

    public void setCurrency (String Currency)
    {
        this.Currency = Currency;
    }

    public String getIBAN ()
    {
        return IBAN;
    }

    public void setIBAN (String IBAN)
    {
        this.IBAN = IBAN;
    }

    public String getAccountType ()
    {
        return AccountType;
    }

    public void setAccountType (String AccountType)
    {
        this.AccountType = AccountType;
    }

    @Override
    public String toString()
    {
        return "AccountSummaryReply [AccountSubType = "+AccountSubType+", WorkingBalance = "+WorkingBalance+", AccountStatus = "+AccountStatus+", AccountNumber = "+AccountNumber+", BranchCode = "+BranchCode+", AccountName = "+AccountName+", OpenActualBalance = "+OpenActualBalance+", Currency = "+Currency+", IBAN = "+IBAN+", AccountType = "+AccountType+"]";
    }}

OperativeAccounts回复:

public class OperativeAccountsReply {

    @JacksonXmlProperty(localName = "AccountSummaryReply")
    private AccountSummaryReply[] AccountSummaryReply;

    public AccountSummaryReply[] getAccountSummaryReply ()
    {
        return AccountSummaryReply;
    }

    public void setAccountSummaryReply (AccountSummaryReply[] AccountSummaryReply)
    {
        this.AccountSummaryReply = AccountSummaryReply;
    }

    @Override
    public String toString()
    {
        return "OperativeAccountsReply [AccountSummaryReply = "+AccountSummaryReply+"]";
    }
}

客户帐户回复:

public class CustomerAccountsReply {

        @JacksonXmlProperty(localName = "FinanceAccountsReply")
        private FinanceAccountsReply FinanceAccountsReply;

        @JacksonXmlProperty(localName = "CustomerNumber")
        private String CustomerNumber;

        @JacksonXmlProperty(localName = "InvestmentAccountsReply")
        private InvestmentAccountsReply InvestmentAccountsReply;

        @JacksonXmlProperty(localName = "ReturnCode")
        private String ReturnCode;

        @JacksonXmlProperty(localName = "OperativeAccountsReply")
        private OperativeAccountsReply OperativeAccountsReply;

        @JacksonXmlProperty(localName = "ReferenceNum")
        private String ReferenceNum;

        @JacksonXmlProperty(localName = "ReturnText")
        private String ReturnText;

        public FinanceAccountsReply getFinanceAccountsReply ()
        {
            return FinanceAccountsReply;
        }

        public void setFinanceAccountsReply (FinanceAccountsReply FinanceAccountsReply)
        {
            this.FinanceAccountsReply = FinanceAccountsReply;
        }

        public String getCustomerNumber ()
        {
            return CustomerNumber;
        }

        public void setCustomerNumber (String CustomerNumber)
        {
            this.CustomerNumber = CustomerNumber;
        }

        public InvestmentAccountsReply getInvestmentAccountsReply ()
        {
            return InvestmentAccountsReply;
        }

        public void setInvestmentAccountsReply (InvestmentAccountsReply InvestmentAccountsReply)
        {
            this.InvestmentAccountsReply = InvestmentAccountsReply;
        }

        public String getReturnCode ()
        {
            return ReturnCode;
        }

        public void setReturnCode (String ReturnCode)
        {
            this.ReturnCode = ReturnCode;
        }

        public OperativeAccountsReply getOperativeAccountsReply ()
        {
            return OperativeAccountsReply;
        }

        public void setOperativeAccountsReply (OperativeAccountsReply OperativeAccountsReply)
        {
            this.OperativeAccountsReply = OperativeAccountsReply;
        }

        public String getReferenceNum ()
        {
            return ReferenceNum;
        }

        public void setReferenceNum (String ReferenceNum)
        {
            this.ReferenceNum = ReferenceNum;
        }

        public String getReturnText ()
        {
            return ReturnText;
        }

        public void setReturnText (String ReturnText)
        {
            this.ReturnText = ReturnText;
        }

        @Override
        public String toString()
        {
            return "CustomerAccountsReply [FinanceAccountsReply = "+FinanceAccountsReply+", CustomerNumber = "+CustomerNumber+", InvestmentAccountsReply = "+InvestmentAccountsReply+", ReturnCode = "+ReturnCode+", OperativeAccountsReply = "+OperativeAccountsReply+", ReferenceNum = "+ReferenceNum+", ReturnText = "+ReturnText+"]";
        }
    }

将 xml 解析为类的代码是:

public static AHBESBFetchAllAccountsFullResponse parseXMLResponseForGetAllAccounts(byte[] response)
            throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper objectMapper;
        AHBESBFetchAllAccountsFullResponse getAllAccountsResponse = null;
        System.err.println(response);
        objectMapper = new XmlMapper();
        getAllAccountsResponse = objectMapper.readValue(StringUtils.toEncodedString(response, StandardCharsets.UTF_8),
                AHBESBFetchAllAccountsFullResponse.class);
        System.err.println(getAllAccountsResponse.getResponseBody().getCustomerAccountsReply().getCustomerNumber());
        return getAllAccountsResponse;
    }

响应 xml 是:

<HB_EAI_REPLY>
    <HB_EAI_HEADER>
        <MsgFormat> FULL.ACCOUNT.INQUIRY </MsgFormat>
        <MsgVersion>0000</MsgVersion>
        <RequestorId>HB</RequestorId>
        <RequestorChannelId>IB</RequestorChannelId>
        <RequestorUserId/>
        <RequestorLanguage>E</RequestorLanguage>
        <RequestorSecurityInfo/>
        <EaiReference>0</EaiReference>
        <ReturnCode>0000</ReturnCode>
    </HB_EAI_HEADER>
    <Reply>
        <CustomerAccountsReply>
            <ReferenceNum>20100407183503799</ReferenceNum>
            <CustomerNumber>1234567</CustomerNumber>
            <ReturnText>OK</ReturnText>
            <OperativeAccountsReply>
                <AccountSummaryReply >  
                    <AccountNumber>545545453</AccountNumber>
                    <IBAN>AE12345678901234567890123456789012</IBAN>
                    <AccountType> </AccountType>
                    <AccountSubType />  
                    <AccountStatus>SA</AccountStatus>
                    <BranchCode>AE0010004</BranchCode>
                    <AccountName> Mohammad </AccountName>
                    <Currency >011</Currency >
                    <OpenActualBalance/>
                    <WorkingBalance > </WorkingBalance >
                </AccountSummaryReply>
                <AccountSummaryReply>   
                    <AccountNumber>545545453</AccountNumber>
                    <IBAN>AE12345678901234567890123456789012</IBAN>
                    <AccountType>Mohammad</AccountType >
                    <AccountSubType />  
                    <AccountStatus>SA</AccountStatus >
                    <BranchCode>AE0010004</BranchCode>
                    <AccountName/>
                    <Currency>011</Currency >
                    <OpenActualBalance>00</OpenActualBalance >
                    <WorkingBalance>AED</WorkingBalance >
                </AccountSummaryReply>
                </OperativeAccountsReply>
                    <InvestmentAccountsReply>
                        <InvestmentContract>
                            <InvestmentContractNo>0903123243</InvestmentContractNo>
                            <InvestmentHolderName>This is the holder’s Name</InvestmentHolderName>
                                    <InvestmentAccountType>One Year Term Deposit</InvestmentAccountType>
                                    <InvestmentBranchCode>AE0010004</InvestmentBranchCode>
                                    <InvestmentCurrencyCode>AED</InvestmentCurrencyCode>
                                    <InvestmentAmount>100000</InvestmentAmount>
                                    <InvestmentOpenDate>20090207</InvestmentOpenDate>
                                    <MaturityDate>20100207</MaturityDate>
                                    <DepositTenor>12</DepositTenor>
                                    <CategoryID>21016</CategoryID>
                                </InvestmentContract>
                                <InvestmentContract>
                            <InvestmentContractNo>0903123243</InvestmentContractNo>
                            <InvestmentHolderName>This is the holder’s Name</InvestmentHolderName>
                                    <InvestmentAccountType>One Year Term Deposit</InvestmentAccountType>
                                    <InvestmentBranchCode>AE0010004</InvestmentBranchCode>
                                    <InvestmentCurrencyCode>AED</InvestmentCurrencyCode>
                                    <InvestmentAmount>100000</InvestmentAmount>
                                    <InvestmentOpenDate>20090207</InvestmentOpenDate>
                                    <MaturityDate>20100207</MaturityDate>
                                    <DepositTenor>12</DepositTenor>
                                    <CategoryID>21016</CategoryID>
                                </InvestmentContract>
                            </InvestmentAccountsReply>
                            <FinanceAccountsReply>
                                <FinanceContract>
                                    <LoanContractNumber>LD0831601635</LoanContractNumber>
                                    <FinanceHolderName>This is the name of Finance Holder</FinanceHolderName>
                                    <FinanceBranchCode>AE0010004</FinanceBranchCode>
                                    <FinanceCurrencyCode>AED</FinanceCurrencyCode>
                                    <FinanceOpenDate>20081106</FinanceOpenDate>
                                    <InstallmentAmount>11974</InstallmentAmount>
                                    <OutstandingDueAmount>1939858.9</OutstandingDueAmount>
                                    <InitialLoanAmount>1127100</InitialLoanAmount>
                                    <DueDate>20100701</DueDate>
                                    <RemainingNoOfInstallments>162</RemainingNoOfInstallments>
                                    <InitialNoOfInstallments>180</InitialNoOfInstallments>
                                    <LoanType>Retail Tawaruq</LoanType>
                                    <NumOfDaysPastDuedate>32</NumOfDaysPastDuedate>
                                </FinanceContract>
                                <FinanceContract>
                                    <LoanContractNumber>LD0909004393</LoanContractNumber>
                                    <FinanceHolderName>This is the name of Finance Holder</FinanceHolderName>
                                    <FinanceBranchCode>AE0010004</FinanceBranchCode>
                                    <FinanceCurrencyCode>AED</FinanceCurrencyCode>
                                    <FinanceOpenDate>20090329</FinanceOpenDate>
                                    <InstallmentAmount>-301732.95</InstallmentAmount>
                                    <OutstandingDueAmount>1360190.99</OutstandingDueAmount>
                                    <InitialLoanAmount>2025000</InitialLoanAmount>
                                    <DueDate>20100930</DueDate>
                                    <RemainingNoOfInstallments>2</RemainingNoOfInstallments>
                                    <InitialNoOfInstallments>3</InitialNoOfInstallments>
                                    <LoanType>Leasehold - Regular Forward Ijara</LoanType>
                                    <NumOfDaysPastDuedate>32</NumOfDaysPastDuedate>
                                </FinanceContract>
                                </FinanceAccountsReply>
                                <ReturnCode>0000</ReturnCode>
                            </CustomerAccountsReply>
                            </Reply>    
                            </HB_EAI_REPLY>

我收到以下错误:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.omnia.pie.esb.outward.messages.accounts.AccountSummaryReply] from String value ('545545453'); no single-String constructor/factory method
 at [Source: java.io.StringReader@59494225; line: 1, column: 563] (through reference chain: com.omnia.pie.esb.outward.messages.accounts.AHBESBFetchAllAccountsFullResponse["Reply"]->com.omnia.pie.esb.outward.messages.accounts.AHBESBFetchAllAccountsResponseBody["CustomerAccountsReply"]->com.omnia.pie.esb.outward.messages.accounts.CustomerAccountsReply["OperativeAccountsReply"]->com.omnia.pie.esb.outward.messages.accounts.OperativeAccountsReply["AccountSummaryReply"]->Object[][0])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:878)
    at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:281)
    at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:284)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1176)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:145)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:156)
    at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:17)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
    at com.omnia.pie.esb.ws.ESBInterchangeControllerImpl.parseXMLResponseForGetAllAccounts(ESBInterchangeControllerImpl.java:190)
    at com.omnia.pie.esb.ws.ESBInterchangeControllerImpl.main(ESBInterchangeControllerImpl.java:147)

最佳答案

看起来 AccountSummaryReply 数组已在您的 XML 中展开。它通常看起来像:

<AccountSummaryReplies>
   <AccountSummaryReply>
      <AccountNumber>545545453</AccountNumber>
   </AccountSummaryReply>
   <AccountSummaryReply>
      <AccountNumber>545545454</AccountNumber>
   </AccountSummaryReply>
</AccountSummaryReplies>

您必须指定数组在 XML 中显示为未包装:

@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "AccountSummaryReply")
private AccountSummaryReply[] AccountSummaryReply;

作为旁注,请考虑使用以下方式配置您的 XmlMapper:

mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);

这意味着 XML 中的所有属性名称都以大写字母开头,并允许跳过所有 @JacksonXmlProperty(localName = ) 注释

关于java - 将 xml 解析为 java 对象时没有单字符串构造函数/工厂方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45669349/

相关文章:

java - Java 代码模板的 Eclipse 自定义变量

java - 显示/隐藏 RichFaces 组件 onclick 客户端? (没有 AJAX)

java - 请告诉我这个jsp程序哪里出错了

javascript - 如何警告 json 数组中的值?

json - FOR JSON PATH 与 FOR JSON AUTO SQL Server

java - 如何在单独的模块中加载多个 Spring ApplicationContext XML?

java - 为什么正则表达式仅在存在其他字符串时匹配字符串?

python - Django 中的请求

c# - 当存在 BOM 时,反序列化 UTF-8 编码的 XML 失败

c# - XML 文档 (2, 2) 中存在错误。这是什么意思?