Search code examples
jakarta-eejerseymoxy

How to make Eclipselink's moxy marshal HashMap<String, String>?


I get the following exception when a method in my jersey application returns a HashMap

[Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred marshalling the object
Internal Exception: Exception [EclipseLink-25007] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor for class java.util.HashMap was not found in the project.  For JAXB, if the JAXBContext was bootstrapped using TypeMappingInfo[] you must call a marshal method that accepts TypeMappingInfo as an input parameter.]

The method in question is:

@PUT
@Path("/subscriptions/{id}")
@Produces("application/json")
public Map<String, String> updateSubscription(@PathParam("id") int id) {
  Map<String, String> response = new HashMap<String, String>();
  response.put("status", "ok");
  return response;
}

Moxy works well with POJOs in my application. So, I am guessing that it is smart enough to handle HashMap.


Solution

  • Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    MOXyJsonProvider does not currently support a root level Map. I have just checked in a fix to the EclipseLink 2.5.2 and 2.6.0 streams so that isReadable/isWriteable will return false for these types so that MOXy will not be picked up as the JSON-binding provider for this use case. You can download a nightly build from the following link starting October 11, 2013:

    I have also entered the following enhancement request to have first class support for Map added: