Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6242549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:58:31+00:00 2026-05-24T11:58:31+00:00

I am developing a JAX-RS webservice using RestEasy 2.2.2 to be deployed to Tomcat

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:58:34+00:00Added an answer on May 24, 2026 at 11:58 am

    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.

    @XmlRootElement 
    public class Root 
    { 
      private String name; 
      private int    junk; 
    
      @XmlJavaTypeAdapter(MapAdapter.class) 
      private Map<String, Boolean> lights; 
    
      public Root() 
      { 
        name = ""; junk = 0; lights = new HashMap<String, Boolean>(); 
      } 
    
      public void   setName(String newName)  { name = newName; } 
      public String getName()                { return name; } 
    
      public void setJunk(int newJunk)  { junk = newJunk; } 
      public int  getJunk()             { return junk; } 
    
      public void turnLightOn(String lightName)   { lights.put(lightName, true); } 
      public void turnLightOff(String lightName)  { lights.put(lightName, false); } 
    } 
    
    class MapAdapter extends XmlAdapter<MapElements[], Map<String, Boolean>> 
    { 
      public MapElements[] marshal(Map<String, Boolean> arg0) throws Exception 
      { 
        MapElements[] mapElements = new MapElements[arg0.size()]; 
    
        int i = 0; 
        for (Map.Entry<String, Boolean> entry : arg0.entrySet()) 
          mapElements[i++] = new MapElements(entry.getKey(), entry.getValue()); 
    
        return mapElements; 
      } 
    
      public Map<String, Boolean> unmarshal(MapElements[] arg0) throws Exception 
      { 
        Map<String,Boolean> r = new HashMap<String,Boolean>(); 
        for(MapElements mapelement : arg0) 
          r.put(mapelement.key, mapelement.value); 
        return r; 
      } 
    } 
    
    class MapElements 
    { 
      @XmlElement public String  key; 
      @XmlElement public Boolean value; 
    
      private MapElements() {} //Required by JAXB 
    
      public MapElements(String key, Boolean value) 
      { 
        this.key   = key; 
        this.value = value; 
      } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on developing a JAX-RS webservice using RestEasy 2.2.2 to be deployed
I'm developing a SOAP service using JAX-WS and JAXB under Netbeans 6.8, and getting
I am developing a RESTful web service using JAX-RS. I am using JAXB to
I am developing a Java webservice application (with JAX-WS) that has to use two
I am currently developing a webservice using jax-ws based on an EJB behind a
I'm getting started in developing web services using JAX-WS. I'm trying to implement classes
I'm interested in using Apache's JAX-RS implementation (CXF) in a Tomcat environment. The documentation
I am currently developing a few Web services using the JAX-WS reference implementation (version
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
I am developing java web services (JAX-WS) to insert data into mysql DB and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.