JAXB Unmarshalling - 找不到具有默认根元素的描述符....

问题描述 投票:0回答:1

我有一个问题是将api xml响应解组到我创建的POJO中。

我确信JAXB上下文知道我的课程,因为我能够正确地编组它。

POJO

package com.bnh.element.misc.Requests;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "selectionResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class SelectionResponse {
    @XmlElement
    boolean _hasErrors;
    @XmlElement
    int selectionIndex;
    @XmlElement
    String type;

}

试图解散它:

Object response = JAXB_CONTEXT.createUnmarshaller()
    .unmarshal( new StringReader(xml) );

抛出异常

[Exception [EclipseLink-25008](Eclipse Persistence Services - 2.6.4.v20160829-44060b6):org.eclipse.persistence.exceptions.XMLMarshalException异常描述:在项目中找不到具有默认根元素{http://tripos.vantiv.com/2014/09/TriPos.Api} selectionResponse的描述符]在org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:1110)

来自API的实际响应:

<selectionResponse
  xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://tripos.vantiv.com/2014/09/TriPos.Api">
  <_errors />
  <_hasErrors>false</_hasErrors>
  <_links />
  <_logs
    xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" i:type="Logs" />
    <_type>selectionResponse</_type>
    <_warnings />
    <selectionIndex>0</selectionIndex>
  </selectionResponse>

对象编组时生成的字符串:

<selectionResponse
    xmlns:ns0="http://tripos.vantiv.com/2014/09/TriPos.Api">
    <_hasErrors>false</_hasErrors>
    <selectionIndex>0</selectionIndex>
</selectionResponse>

任何帮助,将不胜感激。谢谢!

java xml namespaces jaxb unmarshalling
1个回答
0
投票

你的错误说明了很多:

异常描述:在项目中找不到具有默认根元素{http://tripos.vantiv.com/2014/09/TriPos.Api} selectionResponse的描述符]

xml中的默认xmlns是:

  xmlns="http://tripos.vantiv.com/2014/09/TriPos.Api">

它不能“猜到”所以将名称空间添加到根元素声明中,如:

@XmlRootElement(name = "selectionResponse",
    namespace="http://tripos.vantiv.com/2014/09/TriPos.Api")
@XmlAccessorType(XmlAccessType.FIELD)
public static class SelectionResponse {
...
© www.soinside.com 2019 - 2024. All rights reserved.