users@jersey.java.net

[Jersey] Re: Unmarshalling JSON using MoxyJsonProvider

From: Aris Alexis <aris.alexis.gia_at_gmail.com>
Date: Fri, 1 Aug 2014 21:42:54 +0300

Hi sorry for the late response.

You are correct the Activity class is just a simple pojo with
getters/setters and a default constructor with null arguments.
Again you are right that my endpoint returns the pojo but it's not just
echo, maybe there were some changes there (endpoint decides if something is
valid etc). Tags is just a random property I set, it's an example , I
normally set all the pojo's properties.

cheers

Best Regards,
Aris Giachnis


On Wed, Jul 30, 2014 at 6:32 PM, John Brooks <iresprite_at_gmail.com> wrote:

> Thanks for the example, Aris! Hm, I don't see the Activity class in the
> Jersey or MOXy APIs, so I'm inferring that this is the POJO class. Is that
> correct? And setTags is just a way to initialize the data?
>
> Also, I'm guessing that the endpoint at /activities has been coded to just
> echo back the JSON object sent to it; am I reading that correctly?
>
> Again, thanks for your time! It's pretty exciting getting my teeth back
> into Java.
>
> John
>
>
> On Wed, Jul 30, 2014 at 8:19 AM, Aris Alexis <aris.alexis.gia_at_gmail.com>
> wrote:
>
>> I think you are making it more complicated than it really is.
>>
>> 1)Make a POJO (normal object with getters and setters)
>> 2)Include media-moxy maven package in your project
>> 3)write a test for your service like:
>>
>> Activity=new Activity();
>> activity.setTags(tags);
>>
>> Client client =
>> ClientBuilder.newBuilder().register(MoxyJsonFeature.class).build();
>>
>> Entity<Activity> ent= Entity.entity(activity,
>> MediaType.APPLICATION_JSON);
>> Response response =
>> client.target(hostname+"/activities").request(MediaType.APPLICATION_JSON).header("Cookie",
>> getSessionId()).post(ent);
>> assertEquals(Response.Status.OK.getStatusCode(),
>> response.getStatus());
>> assertEquals(true,response.hasEntity());
>>
>> //COMMENT JSONObject jo=new
>> JSONObject(response.readEntity(String.class));
>>
>> Activity retAct=response.readEntity(Activity.class);
>> assertEquals(activity,retAct);
>>
>> Best Regards,
>> Aris Giachnis
>>
>>
>> On Wed, Jul 30, 2014 at 2:21 AM, John Brooks <iresprite_at_gmail.com> wrote:
>>
>>> Hi, all- I'm trying to get my head around how to take JSON and map it to
>>> an object using MOXyJsonProvider. I'm using the Planetside 2 API for a toy
>>> project using this page
>>> <http://census.soe.com/get/ps2:v2/character/5428010618020694593> as my
>>> test. I used the documentation to get me this far:
>>>
>>> http://pastebin.com/jQ9LuCRe
>>>
>>> So I can print my response successfully as a string, but I don't fully
>>> understand how to write it to a POJO. I've seen a couple of examples of
>>> unmarshalling JSON to an object, and it looks like I create a public class
>>> that has members that map to the elements of the JSON array. (I got that
>>> bit of information from Blaise Doughan's blog here
>>> <http://blog.bdoughan.com/2013/07/eclipselink-moxy-and-java-api-for-json.html> and
>>> here
>>> <http://blog.bdoughan.com/2012/03/moxy-as-your-jax-rs-json-provider_15.html>.
>>> From what I can see, I create a class like so:
>>>
>>> public class Character {
>>> private Character_list character_list;
>>> private int returned;
>>> private Timing timing;
>>>
>>> }
>>>
>>> With Character_list and Timing being classes that contain the necessary
>>> members that map to the data I want, and so on. I *think* I'm on the
>>> right track, but I'm missing one or two intuitive leaps and could use some
>>> help here. Can someone explain how I get from where I am to being able to
>>> put the resulting JSON string into an object?
>>>
>>> Thanks, and sorry for all the rank beginner stuff here!
>>>
>>> John
>>>
>>
>>
>