我在將 api xml 響應解組為我創(chuàng)建的 POJO 時遇到問題。我確信 JAXB 上下文知道我的類,因為我能夠正確地編組它。POJOpackage 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) );拋出異常[異常 [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>任何幫助,將不勝感激。謝謝!
1 回答

12345678_0001
TA貢獻1802條經(jīng)驗 獲得超5個贊
您的錯誤說明了很多:
異常描述:在項目中未找到具有默認根元素 { http://tripos.vantiv.com/2014/09/TriPos.Api }selectionResponse 的描述符]
xmlns您的 xml 中的默認值是:
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 {
...
添加回答
舉報
0/150
提交
取消