Discussion:
JSON document -> JAXB class ?
Arun Gupta
2012-01-10 02:01:54 UTC
Permalink
Jersey Client API has to suck up a relatively complex JSON document from
a REST endpoint. What are my options for mapping this JSON document to a
corresponding JAXB class ?

Use org.json APIs ? Would be rather time consuming!

Can this JAXB class be generated somehow ?

Guidelines for manual coding from JSON -> JAXB ?

Thanks,
-Arun
--
http://twitter.com/arungupta
http://blogs.oracle.com/arungupta
Tatu Saloranta
2012-01-10 02:15:18 UTC
Permalink
Jersey Client API has to suck up a relatively complex JSON document from a
REST endpoint. What are my options for mapping this JSON document to a
corresponding JAXB class ?
Do you actually need a JAXB (annotated) class? Or just a regular POJO?

If POJO, just write it to match structure of JSON -- after all, JSON
is object notation, meaning that there is natural correlation between
POJO properties, JSON data.

And even for JAXB basic POJOs typically work fine; annotations are
mostly needed to override default behavior.
Use org.json APIs ? Would be rather time consuming!
No there is no need for such extreme measures. :)

-+ Tatu +-
John Yeary
2012-01-10 02:43:11 UTC
Permalink
There is an automatic mapping using POJO to JSON mapping. The simple act of
annotating a class as an @XmlRootElement is sufficient to provide automatic
mapping for JSON, and XML with just setting
@Produces({MediaType.APPLICATION_JSON}), or
@Consumes({MediaType.APPLICATION_JSON}). If you want to change the default
provider, or change the JSON mapping to something like BadgerFish you need
to change the JSONJAXBContext.

I am sure you already saw this, but I will post it anyway.

http://jersey.java.net/nonav/documentation/latest/json.html

John
____________________________

John Yeary
____________________________

<http://javaevangelist.blogspot.com/> <https://twitter.com/jyeary>
<http://www.youtube.com/johnyeary>
<http://www.linkedin.com/in/jyeary>
<https://plus.google.com/112146428878473069965>
<http://www.facebook.com/jyeary>
<http://feeds.feedburner.com/JavaEvangelistJohnYearysBlog>
<http://netbeans.org/people/84414-jyeary>
____________________________

"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt
Post by Tatu Saloranta
Post by Arun Gupta
Jersey Client API has to suck up a relatively complex JSON document from
a
Post by Arun Gupta
REST endpoint. What are my options for mapping this JSON document to a
corresponding JAXB class ?
Do you actually need a JAXB (annotated) class? Or just a regular POJO?
If POJO, just write it to match structure of JSON -- after all, JSON
is object notation, meaning that there is natural correlation between
POJO properties, JSON data.
And even for JAXB basic POJOs typically work fine; annotations are
mostly needed to override default behavior.
Post by Arun Gupta
Use org.json APIs ? Would be rather time consuming!
No there is no need for such extreme measures. :)
-+ Tatu +-
Arun Gupta
2012-01-10 17:11:37 UTC
Permalink
Thanks Tatu and John!

I've started hand-crafting the POJO class with @XmlRootElement. My
question was looking for some utility that can generate this class for
me by introspecting the JSON response. The service endpoint is a well
known endpoint and I only need this on the client-side so @Produces and
@Consumes will not be required.

Seems
Post by John Yeary
There is an automatic mapping using POJO to JSON mapping. The simple
provide automatic mapping for JSON, and XML with just setting
@Produces({MediaType.APPLICATION_JSON}), or
@Consumes({MediaType.APPLICATION_JSON}). If you want to change the
default provider, or change the JSON mapping to something like
BadgerFish you need to change the JSONJAXBContext.
I am sure you already saw this, but I will post it anyway.
http://jersey.java.net/nonav/documentation/latest/json.html
John
____________________________
John Yeary
____________________________
<http://javaevangelist.blogspot.com/> <https://twitter.com/jyeary>
<http://www.youtube.com/johnyeary> <http://www.linkedin.com/in/jyeary>
<https://plus.google.com/112146428878473069965>
<http://www.facebook.com/jyeary>
<http://feeds.feedburner.com/JavaEvangelistJohnYearysBlog>
<http://netbeans.org/people/84414-jyeary>
____________________________
"Far better it is to dare mighty things, to win glorious triumphs,
even though checkered by failure, than to take rank with those poor
spirits who neither enjoy much nor suffer much, because they live in
the gray twilight that knows not victory nor defeat."
-- Theodore Roosevelt
On Mon, Jan 9, 2012 at 6:01 PM, Arun Gupta
Post by Arun Gupta
Jersey Client API has to suck up a relatively complex JSON
document from a
Post by Arun Gupta
REST endpoint. What are my options for mapping this JSON
document to a
Post by Arun Gupta
corresponding JAXB class ?
Do you actually need a JAXB (annotated) class? Or just a regular POJO?
If POJO, just write it to match structure of JSON -- after all, JSON
is object notation, meaning that there is natural correlation between
POJO properties, JSON data.
And even for JAXB basic POJOs typically work fine; annotations are
mostly needed to override default behavior.
Post by Arun Gupta
Use org.json APIs ? Would be rather time consuming!
No there is no need for such extreme measures. :)
-+ Tatu +-
--
http://twitter.com/arungupta
http://blogs.oracle.com/arungupta
John Yeary
2012-01-10 17:16:16 UTC
Permalink
Hello Arun,

That is very interesting. I would like to know what your solution looks
like since I have a similar problem that I have not had the time to look at
yet. I guess I should have read your post more carefully about the client
side.

John
____________________________

John Yeary
____________________________

<http://javaevangelist.blogspot.com/> <https://twitter.com/jyeary>
<http://www.youtube.com/johnyeary>
<http://www.linkedin.com/in/jyeary>
<https://plus.google.com/112146428878473069965>
<http://www.facebook.com/jyeary>
<http://feeds.feedburner.com/JavaEvangelistJohnYearysBlog>
<http://netbeans.org/people/84414-jyeary>
____________________________

"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt
Post by Arun Gupta
Thanks Tatu and John!
question was looking for some utility that can generate this class for me
by introspecting the JSON response. The service endpoint is a well known
will not be required.
Seems
There is an automatic mapping using POJO to JSON mapping. The simple act
automatic mapping for JSON, and XML with just setting
@Produces({MediaType.APPLICATION_JSON}), or
@Consumes({MediaType.APPLICATION_JSON}). If you want to change the default
provider, or change the JSON mapping to something like BadgerFish you need
to change the JSONJAXBContext.
I am sure you already saw this, but I will post it anyway.
http://jersey.java.net/nonav/documentation/latest/json.html
John
____________________________
John Yeary
____________________________
<http://javaevangelist.blogspot.com/> <https://twitter.com/jyeary> <http://www.youtube.com/johnyeary>
<http://www.linkedin.com/in/jyeary> <https://plus.google.com/112146428878473069965>
<http://www.facebook.com/jyeary> <http://feeds.feedburner.com/JavaEvangelistJohnYearysBlog>
<http://netbeans.org/people/84414-jyeary>
____________________________
"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt
Post by Arun Gupta
Jersey Client API has to suck up a relatively complex JSON document
from a
Post by Arun Gupta
REST endpoint. What are my options for mapping this JSON document to a
corresponding JAXB class ?
Do you actually need a JAXB (annotated) class? Or just a regular POJO?
If POJO, just write it to match structure of JSON -- after all, JSON
is object notation, meaning that there is natural correlation between
POJO properties, JSON data.
And even for JAXB basic POJOs typically work fine; annotations are
mostly needed to override default behavior.
Post by Arun Gupta
Use org.json APIs ? Would be rather time consuming!
No there is no need for such extreme measures. :)
-+ Tatu +-
-- http://twitter.com/arunguptahttp://blogs.oracle.com/arungupta
Arun Gupta
2012-01-10 19:27:44 UTC
Permalink
The endpoint is returning the response as:

[
{ },
{ },
{ }
]

Notice, this is an array, not an array nested in an object. Any
suggestions on how the client-side POJO could look like ?

List<Pojo> or Pojo[] from a JAX-RS endpoint return JSON like:

{"pojo"
:
[{"id":"10","text":"some text"},{"id":"20","text":"more text"}]
}

-Arun
Post by John Yeary
Hello Arun,
That is very interesting. I would like to know what your solution
looks like since I have a similar problem that I have not had the time
to look at yet. I guess I should have read your post more carefully
about the client side.
John
____________________________
John Yeary
____________________________
<http://javaevangelist.blogspot.com/> <https://twitter.com/jyeary>
<http://www.youtube.com/johnyeary> <http://www.linkedin.com/in/jyeary>
<https://plus.google.com/112146428878473069965>
<http://www.facebook.com/jyeary>
<http://feeds.feedburner.com/JavaEvangelistJohnYearysBlog>
<http://netbeans.org/people/84414-jyeary>
____________________________
"Far better it is to dare mighty things, to win glorious triumphs,
even though checkered by failure, than to take rank with those poor
spirits who neither enjoy much nor suffer much, because they live in
the gray twilight that knows not victory nor defeat."
-- Theodore Roosevelt
Thanks Tatu and John!
question was looking for some utility that can generate this class
for me by introspecting the JSON response. The service endpoint is
a well known endpoint and I only need this on the client-side so
@Produces and @Consumes will not be required.
Seems
Post by John Yeary
There is an automatic mapping using POJO to JSON mapping. The
sufficient to provide automatic mapping for JSON, and XML with
@Consumes({MediaType.APPLICATION_JSON}). If you want to change
the default provider, or change the JSON mapping to something
like BadgerFish you need to change the JSONJAXBContext.
I am sure you already saw this, but I will post it anyway.
http://jersey.java.net/nonav/documentation/latest/json.html
John
____________________________
John Yeary
____________________________
<http://javaevangelist.blogspot.com/>
<https://twitter.com/jyeary> <http://www.youtube.com/johnyeary>
<http://www.linkedin.com/in/jyeary>
<https://plus.google.com/112146428878473069965>
<http://www.facebook.com/jyeary>
<http://feeds.feedburner.com/JavaEvangelistJohnYearysBlog>
<http://netbeans.org/people/84414-jyeary>
____________________________
"Far better it is to dare mighty things, to win glorious
triumphs, even though checkered by failure, than to take rank
with those poor spirits who neither enjoy much nor suffer much,
because they live in the gray twilight that knows not victory nor
defeat."
-- Theodore Roosevelt
On Mon, Jan 9, 2012 at 9:15 PM, Tatu Saloranta
On Mon, Jan 9, 2012 at 6:01 PM, Arun Gupta
Post by Arun Gupta
Jersey Client API has to suck up a relatively complex JSON
document from a
Post by Arun Gupta
REST endpoint. What are my options for mapping this JSON
document to a
Post by Arun Gupta
corresponding JAXB class ?
Do you actually need a JAXB (annotated) class? Or just a regular POJO?
If POJO, just write it to match structure of JSON -- after all, JSON
is object notation, meaning that there is natural correlation between
POJO properties, JSON data.
And even for JAXB basic POJOs typically work fine;
annotations are
mostly needed to override default behavior.
Post by Arun Gupta
Use org.json APIs ? Would be rather time consuming!
No there is no need for such extreme measures. :)
-+ Tatu +-
--
http://twitter.com/arungupta
http://blogs.oracle.com/arungupta
--
http://twitter.com/arungupta
http://blogs.oracle.com/arungupta
Tatu Saloranta
2012-01-10 20:44:34 UTC
Permalink
Post by Arun Gupta
[
{ },
{ },
{ }
]
Notice, this is an array, not an array nested in an object. Any
suggestions on how the client-side POJO could look like ?
{"pojo"
[{"id":"10","text":"some text"},{"id":"20","text":"more text"}]
}
Possibily, but not necessarily: this wrapping is only done by some
conventions. POJO mapping for example would not add extra "pojo" property,
but rather return array as is. Adding wrapping is an "XMLism", since it is
required with XML; but it is not required from JSON perspective; and as
such this is done by libraries that use XML APIs.

-+ Tatu +-
Arun Gupta
2012-01-10 21:17:52 UTC
Permalink
Thanks Tatu!

Any suggestions on how this "XMLism" can be avoided with Jersey ?

-Arun
Post by Arun Gupta
[
{ },
{ },
{ }
]
Notice, this is an array, not an array nested in an object. Any
suggestions on how the client-side POJO could look like ?
{"pojo"
[{"id":"10","text":"some text"},{"id":"20","text":"more text"}]
}
Possibily, but not necessarily: this wrapping is only done by some
conventions. POJO mapping for example would not add extra "pojo"
property, but rather return array as is. Adding wrapping is an
"XMLism", since it is required with XML; but it is not required from
JSON perspective; and as such this is done by libraries that use XML APIs.
-+ Tatu +-
--
http://twitter.com/arungupta
http://blogs.oracle.com/arungupta
Tatu Saloranta
2012-01-10 21:50:15 UTC
Permalink
Post by Arun Gupta
Thanks Tatu!
Any suggestions on how this "XMLism" can be avoided with Jersey ?
By configuring it to use POJO mapping, for example:

http://jersey.java.net/nonav/documentation/latest/json.html

(5.1 POJO support)

-+ Tatu +-
Arun Gupta
2012-01-10 22:13:00 UTC
Permalink
Got it, this might work!

Is "web.xml" the only way to configure this support on the server side ?
Can I configure it programmatically, something like DefaultClientConfig ?

-Arun
Post by John Yeary
Post by Arun Gupta
Thanks Tatu!
Any suggestions on how this "XMLism" can be avoided with Jersey ?
http://jersey.java.net/nonav/documentation/latest/json.html
(5.1 POJO support)
-+ Tatu +-
--
http://twitter.com/arungupta
http://blogs.oracle.com/arungupta
Tatu Saloranta
2012-01-10 18:16:09 UTC
Permalink
Post by Arun Gupta
Thanks Tatu and John!
question was looking for some utility that can generate this class for me
by introspecting the JSON response. The service endpoint is a well known
will not be required.
Yes, that's not a bad idea; you could have a look at:
https://github.com/wotifgroup/json2pojo
which I think tries to do just that and looks like an interesting project.
And as usual, more usage a lib gets, better it becomes (haven't used it yet
myself but am definitely interested).

There may well be other similar projects, so maybe others can share more
links.

-+ Tatu +-
Loading...