1

How to marshall a List of JAXBElement ?

I've got one POJO I cannot annotate, for instance:

public class APojo {

private String aString;

public APojo() { 
    super(); 
}

public String getAString() {
    return aString;
}

public void setAString(String aString) {
    this.aString = aString;
}
}

So I'm doing this

APojo aPojo = new APojo();
aPojo.setaString("a string");       
JAXBElement<APojo> aJAXBedPojo = new JAXBElement<APojo>(new QName("apojo"), APojo.class, aPojo);

Which is correctly being marshaled.

However

List<JAXBElement<APojo>> list = new ArrayList<JAXBElement<APojo>>();

Is not working: when I' doing this

JAXBContext context = JAXBContext.newInstance(APojo.class, ArrayList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(list, System.out);

The runtime raises:

[com.sun.istack.internal.SAXException2: unable to marshal type "java.util.ArrayList" as an element because it is missing an @XmlRootElement annotation]

Which is normal since ArrayList is not annotated.

I know I can create a wrapper around ArrayList and annotate it with a @XmlRootElement so I can marshall this wrapper.

I'm looking for a solution without such a wrapper. Is it possible to create a JAXBElement with T being an ArrayList ? Or something similar ?

thomas.g
  • 3,894
  • 3
  • 29
  • 36

2 Answers2

2

I would strongly recommend you use an XmlAdapter. You can simply register the adapter with your marshaller and it will use your annotated adapter class in place of the class you can't annotate.

Also, I think a wrapper class would be cleaner than trying to do something without a wrapper.

That being said, if you really wanted to, you could try the following:

List<JAXBElement<APojo>> list = new ArrayList<JAXBElement<APojo>>();
JAXBElement<List<JAXBElement<APojo>>> listElement = new JAXBElement<List<JAXBElement<APojo>>>(new QName("apojolist"), List.class, list);
Pace
  • 41,875
  • 13
  • 113
  • 156
  • In fact I've already tried that but Eclipse tells me then that "The constructor JAXBElement>>(QName, Class, List>) is undefined". I don't understand clearly why :) – thomas.g Mar 16 '12 at 17:05
  • The "right" code must be very close to your answer but I don't find it :( – thomas.g Mar 16 '12 at 17:14
0

The best solution seems to build a wrapper like explained here.

As my goal was to return my list from a REST web service, it was even simpler for me to wrap my list inside a javax.ws.rs.core.GenericEntity, like this

new GenericEntity<List<APojo>>(aPojo) {}
Community
  • 1
  • 1
thomas.g
  • 3,894
  • 3
  • 29
  • 36