JaxB Unmarshal异常:javax.xml.bind.UnmarshalException:意外元素。我哪里出错了?

时间:2014-08-18 12:17:52

标签: java xml jaxb

我有两个bean目录,一个用于请求bean,另一个用于响应bean,每个目录都包含自己的ObjectMapper类。

我能够成功地将我的XML Bean(对于请求)编组为XML String,然后使用下面的代码将其发送出去。

    JAXBElement<RequestblockType> requestblock = objectFactory.createRequestblock(requestblockType);
    m.marshal(requestblock, writer);
    // output string to console
    byte[] bytes = writer.toString().getBytes(Charset.forName("UTF-8"));

我从服务器返回我的响应作为xml字符串,现在尝试解组回我的对象​​。

    JAXBContext jaxbContext = JAXBContext.newInstance(ResponseblockType.class);
    StringReader reader = new StringReader(xmlString);
    jaxbContext.createUnmarshaller().unmarshal(reader); //this line throws Exception
    um.unmarshal(reader);

在指定的行上我得到以下异常:

javax.xml.bind.UnmarshalException:意外元素(uri:&#34;&#34;,local:&#34; responseblock&#34;)。预期元素为&lt; {} responseblockType&gt;

下面是我的XML和最重要的顶级bean类

<?xml version="1.0" encoding="UTF-8"?>
<responseblock version="3.67">
  <requestreference>X5727835</requestreference>
   <response type="AUTH">
  <merchant>
     <merchantname>Merchant1</merchantname>
     <orderreference>8900001233</orderreference>
     <tid>12341234</tid>
     <merchantnumber>00000000</merchantnumber>
     <merchantcountryiso2a>GB</merchantcountryiso2a>
  </merchant>
  <transactionreference>3-9-1598316</transactionreference>
  <timestamp>2014-08-18 11:56:40</timestamp>
  <acquirerresponsecode>00</acquirerresponsecode>
  <operation>
     <accounttypedescription>ECOM</accounttypedescription>
  </operation>
  <settlement>
     <settleduedate>2014-08-18</settleduedate>
     <settlestatus>0</settlestatus>
  </settlement>
  <billing>
     <amount currencycode="USD">3698</amount>
     <payment type="VISA">
        <issuer>Test Issuer1</issuer>
        <pan>1234123412341234</pan>
        <issuercountry>US</issuercountry>
     </payment>
     <dcc enabled="0" />
  </billing>
  <authcode>TEST</authcode>
  <live>0</live>
  <error>
     <message>Ok</message>
     <code>0</code>
  </error>
  <security>
     <postcode>0</postcode>
     <securitycode>2</securitycode>
     <address>0</address>
  </security>
   </response>
</responseblock>

package com.test.adapter.payment.test1.client.model.beans.responses;

导入javax.xml.bind.annotation。*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "responseblockType")
@XmlRootElement
public class ResponseblockType {

@XmlElement(required = true)
protected String requestreference;
@XmlElement(required = true)
protected ResponseType response;
@XmlAttribute(name = "version")
protected String version;

public String getRequestreference() { return requestreference; }
public void setRequestreference(String value) { this.requestreference = value; }
public ResponseType getResponse() { return response; }
public void setResponse(ResponseType value) { this.response = value; }
public String getVersion() { return version; }
public void setVersion(String value) { this.version = value;  }

}

任何人都可以告诉我哪里出错了吗? 如果可能,我可以提供更多信息。我从其他帖子中看到,将@XMLRootElement添加到我的顶级bean响应类可能有所帮助,但它没有。在编组时我不需要为我的请求做这个。

1 个答案:

答案 0 :(得分:2)

序列化XML响应块的根元素名称与其中定义的名称不同 豆类。

<responseblock version="3.67">

@XmlRootElement
public class ResponseblockType

匹配您的注释应该如下:

 <responseblockType version="3.67">
    ...
    ...
 </responseblockType>

或者您可以覆盖@XmlRootElement注释中的默认根元素名称:

@XmlRootElement(name="responseblock")
public class ResponseblockType   

另一个问题是,在xml中,根元素响应块最后没有正确关闭。同样的问题似乎适用于在xml的第4行打开的元素响应

 line 4: <response type="AUTH">

但未关闭。