Need help understanding how to marshal "custom" JSON into a pojo

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Need help understanding how to marshal "custom" JSON into a pojo

David M. Karr
I've written several small CXF JAX-RS services where I either designed it "code first" or "schema first".  In both cases, the resulting json was "conventional", so mapping from the json to pojos happened automatically.

I'm now trying to write some client code that gets json data in an "unconventional" form.  I don't understand my options for customizing this process.  I think it's pretty clear I'll have to write a MessageBodyReader, and I'll probably be doing something with a JSON provider, but that's about all I can see.

I posted about this at http://stackoverflow.com/questions/43728177/map-unconventional-json-to-pojo-with-cxf-jax-rs-client .

In short, my json data looks something like this:
------------
{
    "addOnOffers": [
        {
            "offer": {
                "offerId": 4000
            }
        }
    ]
}
---------

If I have to, I'll just get the json string from the response and use a plain json parser to extract the data, but I'd like to understand how to do this in the MessageBodyReader, and with the conventional facilities provided by CXF.
Reply | Threaded
Open this post in threaded view
|

Re: Need help understanding how to marshal "custom" JSON into a pojo

Andy McCright
Hi David,

Here is a sample MessageBodyReader (and also Writer) that uses JSON-P to
parse some "custom" JSON code:
https://github.com/eclipse/microprofile-conference/blob/master/microservice-vote/src/main/java/io/microprofile/showcase/vote/api/AttendeeProvider.java

If you are writing this in a client, then you will need to register your
MBR with your client like this:
Client c = ClientBuilder.newClient();
c.register(AttendeeProvider.class);

Hope this helps,
Andy


On Tue, May 2, 2017 at 11:01 AM KARR, DAVID <[hidden email]> wrote:

> I've written several small CXF JAX-RS services where I either designed it
> "code first" or "schema first".  In both cases, the resulting json was
> "conventional", so mapping from the json to pojos happened automatically.
>
> I'm now trying to write some client code that gets json data in an
> "unconventional" form.  I don't understand my options for customizing this
> process.  I think it's pretty clear I'll have to write a MessageBodyReader,
> and I'll probably be doing something with a JSON provider, but that's about
> all I can see.
>
> I posted about this at
> http://stackoverflow.com/questions/43728177/map-unconventional-json-to-pojo-with-cxf-jax-rs-client
> .
>
> In short, my json data looks something like this:
> ------------
> {
>     "addOnOffers": [
>         {
>             "offer": {
>                 "offerId": 4000
>             }
>         }
>     ]
> }
> ---------
>
> If I have to, I'll just get the json string from the response and use a
> plain json parser to extract the data, but I'd like to understand how to do
> this in the MessageBodyReader, and with the conventional facilities
> provided by CXF.
>