gpt4 book ai didi

java - JaxB Unmarshal Exception : javax. xml.bind.UnmarshalException : unexpected element. 我哪里出错了?

转载 作者:行者123 更新时间:2023-11-30 09:04:06 25 4
gpt4 key购买 nike

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

在发送之前,我能够成功地将我的 XML Beans(用于请求)编码为 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 中,根元素 responseblock 最后没有正确关闭。同样的问题似乎适用于在您的 xml 第 4 行打开的元素响应

 line 4: <response type="AUTH">

但没有关闭。

关于java - JaxB Unmarshal Exception : javax. xml.bind.UnmarshalException : unexpected element. 我哪里出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25363114/

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