users@jersey.java.net

[Jersey] Re: Get JSON as plain string

From: Ryan Stewart <zzantozz_at_gmail.com>
Date: Fri, 11 Feb 2011 06:30:45 -0600

Maybe I'm misunderstanding here, but it sounds like you just want to pull
the request entity out before it even hits JAXB. For an example of how to do
that, see
http://java.net/projects/jersey/sources/svn/content/trunk/jersey/jersey-server/src/main/java/com/sun/jersey/api/container/filter/LoggingFilter.java,
specifically around line 163. Basically, you just read in the entity, do
what you want with it, then replace the original stream with a new one so
that later stuff (JAXB) can still read it. I'm doing this for logging
purposes, and it works great. Just keep in mind that you'll be storing
entire request entities in memory (if that isn't already happening), which
could give you heap problems.

On Fri, Feb 11, 2011 at 4:47 AM, Paul Sandoz <Paul.Sandoz_at_oracle.com> wrote:

> Hi Arseni,
>
> My guess is that is quite tricky to do with JAXB because it uses a SAX
> parser, and thus the XML or JSON is already parsed and JAXB does not have
> access to the underlying characters.
>
> I don't think an XmlAdapter will do it, because it does not have access to
> the underlying characters and it also does not know if the source was XML or
> JSON.
>
> For your case you might want to see if Jackson can do it. It may require
> that you write your own message body reader/writer for the type in question
> using the lower level JSON parsing if Jackson cannot do it at the binding
> level.
>
> Paul.
>
> On Feb 11, 2011, at 9:57 AM, Arseni Grigorjev wrote:
>
> Hi,
>
> I have problems figuring out how to dig out a plain JSON string from a
> request.
>
> We're currently using Jersey 1.5.1 + Spring for handling JSON requests, and
> the request structure looks something like this:
>
> {
> "id": 34324242,
> "foo": "bar",
> "info": {
> "infofield1": "some value",
> "infofield2": "some other value",
> "infodetails": {
> "details1": "aaaa",
> "details2": "bbbb"
> }
> }
> }
>
> The Java class to which this request would map (ideally) looks like this:
>
> @XmlRootElement
> public class FooBarRequest {
>
> public Integer id;
>
> public String foo;
>
> public String infoJson;
> }
>
> The idea is, that we want to get "info" as plain json string (not parsed to
> a any java object structure) to store it directly to DB as a BLOB. This
> doesn't seem to work out-of-a-box for Jersey 1.5.1, we are currently trying
> to upgrade to Jersey version 1.6, but maybe you have some tips on how to do
> that?
>
> Maybe there is a ready XmlAdapter, which would perform something like this
> for us?
> If not, does anyone know, how to write one for this particular case?
>
> Best regards,
> Arseni.
>
>
>
>