Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with Eclipselink and @XmlRef

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.
like image 467
user2214717 Avatar asked Jun 18 '13 17:06

user2214717


1 Answers

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.

like image 129
bdoughan Avatar answered Sep 28 '22 23:09

bdoughan