6

I am using Eclipselink 2.3.2 as my JAXB (JSR-222) provider. I have created a generic list which consists of a list of items and a set of Pagination Links.

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name = "listdata")
public class ListEntity<T> {

    @XmlElementRef
    public List<T> data;

    @XmlElementRef
    public PaginationLinks links;

    public ListEntity(List<T> data) {
        this.data = data;
    }

    public ListEntity() {
    }

}

My actual Entity

@XmlRootElement(name="authorization")
public class AuthorizationDTO {

    @XmlElement 
    public String referenceNumber;

} 

So, after creation of the list, when I try to marshall it, I get the following error. Works fine with @XmlElement for List data but obviously cannot be used as it creates the Object representation

Caused by: Exception [EclipseLink-50006] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.JAXBException

Exception Description: Invalid XmlElementRef on property data on class com.ofss.fc.botg.infra.model.ListEntity. Referenced Element not declared.
bdoughan
  • 147,609
  • 23
  • 300
  • 400

1 Answers1

5

The @XmlElementRef annotation has the following requirements (see: http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/annotation/XmlElementRef.html):

  • If the collection item type (for collection property) or property type (for single valued property) is JAXBElement, then @XmlElementRef.name() and @XmlElementRef.namespace() must point an element factory method with an @XmlElementDecl annotation in a class annotated with @XmlRegistry (usually ObjectFactory class generated by the schema compiler) :

    • @XmlElementDecl.name() must equal @XmlElementRef.name()
    • @XmlElementDecl.namespace() must equal @XmlElementRef.namespace().
  • If the collection item type (for collection property) or property type (for single valued property) is not JAXBElement, then the type referenced by the property or field must be annotated with @XmlRootElement.


Since ListEntity will be processed as a class and not a type the data field will be treated as having type Object and therefore the requirements for @XmlElementRef will not have been met resulting in the exception that you are seeing.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • I had a similar issue consuming a .NET WCF Service from a jaxws generated client. In the end I had to ask for the offending property to be removed and it worked like a charm after the removal. Would it be better to use an array instead of a list for the response entity ? – ivcuello Dec 08 '20 at 17:42