1

I start understanding how jersey works with JAXB. But today i faced a particular case where i want to marshall a Map of (Date,List) entries:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class MyClass{
  @XmlJavaTypeAdapter(MapAdapter.class)
  private Map<Date,List<MyObject>> = new TreeMap<Date,List<MyObject>>(new DateCompareDesc());
}

The goal here is to marshall a Map whose entry is a Date with its corresponding list of MyObject. the map is sorted in desc order.

For this i implemented an Adapter for Map (MapAdapter, following @Blaise Doughan's tutorial, http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html). The problem is on the Date key. I have an Error : Unable to marshall java.util.Date. So i tried this new Date Adapter :

public class DateAdapter extends XmlAdapter<String, Date> {

@Override
public Date unmarshal(String v) throws Exception {
   //not implemented
}

@Override
public String marshal(Date v) throws Exception {
    return v.toString();
}

}

Where can i add @XmlJavaTypeAdapter(DateAdapter.class) so that Jersey could marhsall Date as key to my TreeMap?

Thanks.

Lahniep
  • 719
  • 1
  • 11
  • 30

1 Answers1

1

JAXB supports the marshalling/unmarshalling of java.util.Date to the standard XML schema types: date, time, dateTime. You can control the type used with the @XmlSchemaType annotation.

If your date information is not represented as one of the standard XML schema types, you can use an XmlAdapter similar to the one I used the following answer to a similar question:

If you need to use the XmlAdapter approach, the @XmlJavaTypeAdapter annotation would be placed on the Date field of the adapted object representing the entry in the Map. Below is what this might look like based on my blog: http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html.

import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

public class MyMapEntryType {

   @XmlAttribute
   @XmlJavaTypeAdapter(DateAdapter.class)
   public Date key;

   public List<MyObject> value;

}
Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Hi Blaise, thanks for your response. In my case i have a Map. Date and List are not comming from another class on which i could add appropriate annotations. So is it not possible to annotate that Map to Adapth the Date and List? – Lahniep Jan 17 '12 at 13:19
  • @Lahniep - You are using the default representation of Map? Also how is your date information formatted? – bdoughan Jan 17 '12 at 13:34
  • Yes .. it is a TreeMap and the date is formated with SimpleDateFormat("yyyy-MM-dd HH:mm:ss") – Lahniep Jan 17 '12 at 13:42
  • If all you Date objects are formatted that way you could register the XmlAdapter at the package level. Here is an example: http://blog.bdoughan.com/2011/05/jaxb-and-joda-time-dates-and-times.html – bdoughan Jan 17 '12 at 13:49
  • I am using Eclipse and i got error"Package annotations must be in file package-info.java" when adding this : @XmlJavaTypeAdapters({@XmlJavaTypeAdapter(type=Date.class,value=DateAdapter.class)}) before package declaration of my java file. ?? – Lahniep Jan 17 '12 at 14:22
  • @Lahniep - Yes the annotations will need to go in a class called `package-info`. In the blog post I linked to the content of this class will look like the code under the "Registering the XmlAdapters" section. – bdoughan Jan 17 '12 at 15:11
  • I've added the package-info.java class and it seems to work on a dummy test class. But when i use it on my Map object i obtain this error : java.lang.NullPointerException at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:169) – Lahniep Jan 18 '12 at 14:04