users@jersey.java.net

Date format based on request parameter

From: Chris Carrier <ctcarrier_at_gmail.com>
Date: Wed, 31 Mar 2010 18:03:03 -0700

Hey folks,

A little while ago I toiled to find a way to deal with dates being
represented as Java timestamps. I finally discovered the ability to
use package level annotations to define custom JAXB adapters based on
type that I could use to format Dates however I wanted. Well now I
have a new requirement to return Date representations based on a
parameter in the get request. Like a parameter might be passed into
GET calls like

...?humanReadableDates=true

And then I return GMT dates or whatever. Otherwise I return
timestamps. So I would love to be able to just use my central
XmlJavaTypeAdapter so that I can apply this easily to any pojo's I
want but the annotation syntax:

@XmlJavaTypeAdapter(value = DateAdapter.class, type = Date.class)

Suggests that JAXB will instantiate a new DateAdapter whenever it
needs one and I have no idea how to get a handle on that instance to
do anything with it based on request parameters.

So basically I was wondering if anyone has any suggestions for a good
way to do this. If there's some trick to get a handle on an
XmlJavaTypeAdapter that would work. I could just put some logic in my
domain classes so that i can set some Date format instance variable in
them to affect the way they represent Dates but that really seems
pretty crappy. I would much prefer to keep my marshalling config
stuff centralized and out of my pojo's. I've got a Provider that
'implements ContextResolver<JAXBContext>' but I see no way to
configure my Date format from here.

If I was using plain Jackson I could do
'mapper.getSerializationConfig().setDateFormat(FORMAT)' but at this
point we've gone done the road of using JAXB and it would be a bit of
a pain to switch.

Suggestions very welcome.

Thanks!
Chris