users@jersey.java.net

[Jersey] Re: Jersey JSON with MongoDB ObjectId

From: Markus Karg <markus.karg_at_gmx.net>
Date: Tue, 8 Feb 2011 20:07:56 +0100

If you know for sure that the class ObjectId can be serialized (which not
necessarily is the case as "implements Serializable" does not really
guarantees that, as it is only a marker interface), then you can register it
with your own ContextResolver. But that is possibly too much work, as you
then also have to register everything and do not gain the auto-registering
running under the hood currently.

Another idea would be to apply @XmlJavaTypeAdapter (see
http://weblogs.java.net/blog/2005/09/30/using-jaxb-20s-xmljavatypeadapter)
which allows you to virtually replace ObjectId by another type which is
known by JAXB. That is simple and short to do. I am using that to replace
@XmlRootElement references by URIs for example.

Regards
Markus

> -----Original Message-----
> From: Mike Key [mailto:mikey_at_zenbitz.com]
> Sent: Dienstag, 8. Februar 2011 19:54
> To: users_at_jersey.java.net
> Subject: [Jersey] Jersey JSON with MongoDB ObjectId
>
> I am trying to produce output of a simple class annotated with JAXB
> annotations that is also used for persisting to a MongoDB.
> Specifically
> one of my elements is a Mongo ObjectId type. ObjectId is a class that
> is Serializable but Jersey (or rather JAXB) does not produce any output
> for this field. I'm wondering what approach I need to take to make
> this
> output show up when I call my Resource. The bean is annotated as such:
>
> @XmlRootElement("foo")
> @XmlAccessorType(XmlAccessType.FIELD)
> public class Foo {
> @XmlElement(name = "id")
> private ObjectId id;
>
> public ObjectId getId() {
> return id;
> }
>
> public void setId(ObjectId id) {
> this.id = id;
> }
> }
>
> So would a ContextResolver be necessary here to ensure the
> ObjectId.toString() method is called? Or is there another way it is
> recommended to produce output for an object I don't have access to
> annotate with JAXB? This seems like a simple and common issue but I
> can't seem to find the solution...probably looking in the wrong place.
>
> Thanks for any pointers.