简体   繁体   中英

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.

I'm sure the JAXB context is aware of my class, because I'm able to marshall it correctly.

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)

The actual Response from the 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]

The default xmlns in your xml is:

  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. 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. 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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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