users@jersey.java.net

Re: [Jersey] Date format based on request parameter

From: Imran M Yousuf <imran_at_smartitengineering.com>
Date: Thu, 1 Apr 2010 08:23:00 +0600

Hi Chris,

In some case of mine, the path I took is I used a ThreadLocal based
storage to store Http* classes from a Jersey resource to be used from
classes needing them but not being able to avail them through DI.
Reading through your contents I would have done some thing similar
with the logic residing in DateAdapter.

Regards,

Imran

On Thu, Apr 1, 2010 at 7:03 AM, Chris Carrier <ctcarrier_at_gmail.com> wrote:
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>



-- 
Imran M Yousuf
Entrepreneur & CEO
Smart IT Engineering
Dhaka, Bangladesh
Email: imran_at_smartitengineering.com
Blog: http://imyousuf-tech.blogs.smartitengineering.com/
Mobile: +880-1711402557