I am developing a JAX-RS webservice using RestEasy 2.2.2 to be deployed to Tomcat 7. The service returns (should return) XML through the use of JAXB. Returned XML should contain representations of ConcurrentHashMap similar to how it is used in the following code:
@XmlRootElement(name="items")
@XmlAccessorType(XmlAccessType.NONE)
public class ItemCollection
{
@XmlElement(name="item")
private ConcurrentHashMap<String, Item> items;
public ItemCollection()
{
items = new ConcurrentHashMap<String, item>();
// fill the map
}
}
The Item class also contains a ConcurrentHashMap that needs to be serialized to XML.
This is the resource class:
@Path("/items")
public class ItemResource
{
@GET
@Produces(MediaType.APPLICATION_XML)
public ItemCollection getAllItems()
{
// get itemManager
return itemManager.getItems(); // ItemManager holds an instance of ItemCollection
}
}
This code runs, but produces an XML with no content:
<items>
<item/>
</items>
What I am trying to get as an output is something like:
<items>
<item id="...">
<data>...</data>
<otheritems>
<otheritem id="...">
<someotherdata>...</someotherdata>
</otheritem>
</otheritems>
</item>
<item id="...">
<data>...</data>
<otheritems>
<otheritem id="...">
<someotherdata>...</someotherdata>
</otheritem>
<otheritem id="...">
<someotherdata>...</someotherdata>
</otheritem>
<otheritem id="...">
<someotherdata>...</someotherdata>
</otheritem>
</otheritems>
</item>
</items>
I found out that a MessageBodyWriter implementation is required where builtin functionality is not sufficient. I tried to come up with a MessageBodyWriter implementation to marshal the ConcurrentHashMap, but I have not been able to get it working so far (i.e. I can get the code being called, but it stops with various exceptions).
It seems I do not quite grasp how MessageBodyWriter (and MessageBodyReader) interfaces should be implemented and used. I have Bill Burke’s “RESTful Java with JAX-RS” book. It is very useful in terms of helping design JAX-RS services but I could not find enough details about the MessageBodyWriter functionality in the related section. My internet searches did not yield anything that could guide me in the right direction, either.
I would appreciate if anyone can help me in figuring out how to implement the MessageBodyWriter (and MessageBodyReader) interfaces properly. I do not know if I am missing an annotation, misplacing one or whether I need a completely new approach.
Thanks in advance for helping.
EDIT:
Modifying the code to following gets me halfway there:
@XmlRootElement(name="items")
@XmlAccessorType(XmlAccessType.NONE)
public class ItemCollection
{
private ConcurrentHashMap<String, Item> items;
public ItemCollection()
{
items = new ConcurrentHashMap<String, item>();
// fill the map
}
@XmlElement(name="item")
public Collection<Item> getItems()
{
return items.values();
}
}
This generates the XML that I need (sample included above). However, this code does not work when it comes to unmarshalling. I get the following exception:
java.lang.UnsupportedOperationException
java.util.AbstractCollection.add(AbstractCollection.java:221)
com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:290)
com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:254)
com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:106)
com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(ArrayERProperty.java:195)
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:507)
com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:145)
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2938)
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142)
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169)
// ... the rest
I assume the reason is the lack of a “proper setter” so that the unmarshaller does not try to add items into a Collection but I do not know how that setter would look like. If anyone knows how to do this, I would appreciate the help.
Thanks in advance.
EDIT 2:
BTW, I have seen Chris’ reply where he suggests using @XmlJavaTypeAdapter. I have tried the suggestion and it gets me close to the XML I need. However, the XML I get using @XmlJavaTypeAdapter has an extra level in it (<class><items><item> instead of <items><item> — as seen in my examples, I have the ConcurrentHashMap instances as a member variable of a class). I also can’t seem to change the element name of the individual map items (they are always called “item”).
These are not big problems and I can make the necessary changes and live with them if necessary. If possible, however, I would like to not have them in the first place. For educational purposes, I would also like to understand why the code in EDIT 1 does not work for unmarshalling (and how to fix it, if possible).
Thanks in advance for all the help.
I think your problem is that you are trying to mix JAXB with your MessageBodyReader/MessageBodyWriter. You have a JAXB object so you do not really want RestEasy to do all of the serialization using your MessageBodyWriter as it would not take into account your JAXB objects. You could do it this way, but you would need to serialize the rest of your object model too.
The MessageBodyReader/Writer is geared for working with Streams which is probably why it did not make much sense. It makes no assumptions that you are going to XML.
What you probably want to do is create an JAXB XmlJavaTypeAdapter for the map and let JAXB do the XML creation. You can find more information on the JavaDoc page:
http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/adapters/XmlJavaTypeAdapter.html
I did find one good post on this over the Metro mailing list here. This code should give you what you want. The context was around JAX-WS but you are looking specifically for the JAXB annotation to do a custom binding.