简体   繁体   English

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

[英]JAXB Unmarshalling - A descriptor with default root element ... was not found

I'm having an issue unmarshalling an api xml response into a POJO that I created.我在将 api xml 响应解组为我创建的 POJO 时遇到问题。

I'm sure the JAXB context is aware of my class, because I'm able to marshall it correctly.我确信 JAXB 上下文知道我的类,因为我能够正确地编组它。

POJO 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;

}

Attempting to unmarshall it:试图解组它:

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

Exception thrown抛出异常

[Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.XMLMarshalException Exception Description: A descriptor with default root element { http://tripos.vantiv.com/2014/09/TriPos.Api }selectionResponse was not found in the project] at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:1110) [异常 [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)

The actual Response from the API :来自 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>

String that gets generated when object is marshalled:编组对象时生成的字符串:

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

Any help would be appreciated.任何帮助,将不胜感激。 Thanks!谢谢!

Your error tells a lot:您的错误说明了很多:

Exception Description: A descriptor with default root element { http://tripos.vantiv.com/2014/09/TriPos.Api }selectionResponse was not found in the project]异常描述:在项目中未找到具有默认根元素 { http://tripos.vantiv.com/2014/09/TriPos.Api }selectionResponse 的描述符]

The default xmlns in your xml is:您的 xml 中的默认xmlns是:

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

It cannot be "guessed" so add the namespace to your root element declaration, like:它不能被“猜测”,因此将命名空间添加到您的根元素声明中,例如:

@XmlRootElement(name = "selectionResponse",
    namespace="http://tripos.vantiv.com/2014/09/TriPos.Api")
@XmlAccessorType(XmlAccessType.FIELD)
public static class SelectionResponse {
...

I got the same error while unmarshalling but in my case the problem was missing @XmlRootElement in java object generated by javax.我在解组时遇到了同样的错误,但在我的情况下,问题是 javax 生成的 java 对象中缺少 @XmlRootElement Since JAXB classes map to complex types, it is possible for a class to correspond to multiple root elements and javax library was not able to identify the correct root element.由于 JAXB 类映射到复杂类型,因此一个类可能对应多个根元素,而 javax 库无法识别正确的根元素。 which in the end was complaining about到底是在抱怨

Exception Description: A descriptor with default root element "" was not found in the project]异常描述:在项目中找不到具有默认根元素“”的描述符]

Hope this will help some one !希望这会有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM