users@jersey.java.net

[Jersey] Re: Jersey JSON with MongoDB ObjectId

From: Mike Key <mikey_at_zenbitz.com>
Date: Tue, 08 Feb 2011 23:33:33 -0700

Markus,

Thanks! This is exactly what I needed and it worked like a charm.

Tatu, thanks for the info. on bson4jackson as well, I am not using the
native BSON from Mongo but rather mapping to Java Beans...although this
may be where I go in the future now that I know such a thing exists.

Great stuff, thanks to both of you!

Mike

> ------------------------------------------------------------------------
>
> Markus Karg <mailto:markus.karg_at_gmx.net>
> February 8, 2011 12:07 PM
>
>
> 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
>
>
>
> ------------------------------------------------------------------------
>
> Mike Key <mailto:mikey_at_zenbitz.com>
> February 8, 2011 11:54 AM
>
>
> 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.
>
>