我有两个 bean 目录,一个用于请求 bean,另一个用于响应 bean,每个目录都包含自己的 ObjectMapper 类。
在使用下面的命令发送之前,我能够成功地将我的 XML Bean(用于请求)编码为 XML 字符串。
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:“”,本地:“responseblock”)。预期元素为 <{}responseblockType>
下面是我的 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>
包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 响应类可能会有所帮助,但事实并非如此。此外,我在编码时不必按照我的要求执行此操作。
最佳答案
序列化 XML 响应 block 的根元素名称与其中定义的不同 bean 类。
<responseblock version="3.67">
但是
@XmlRootElement
public class ResponseblockType
它应该是以下内容以匹配您的注释:
<responseblockType version="3.67">
...
...
</responseblockType>
或者您可以覆盖 @XmlRootElement
注释中的默认根元素名称:
@XmlRootElement(name="responseblock")
public class ResponseblockType
另一个问题是,在 xml 中,根元素响应 block 最终未正确关闭。同样的问题似乎也适用于在 xml 第 4 行打开的元素响应
line 4: <response type="AUTH">
但未关闭。
关于java - JaxB Unmarshal 异常 : javax. xml.bind.UnmarshalException : unexpected element. 我哪里出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25363114/