5

I am using Jersey which was creating an object out of the JSON or xml from the client.

Now, I am using JSONp to create a object. Since jsonp cannot do a post, I am left with sending data using get.

  1. Can I use GET request and still deserialize? I know I can use @QueryParam for each and then somehow deserialize. But like POST and Put, can I just give it a input parameter and expect Jersey to deserialize automaticaly.

  2. How can I deserialize a json into java object using Jersey?

Thanks.

1 Answer 1

10

After some research, it is not difficult to deserialize directly from json. This is how I did it.

Send JSON as a queryparam from the client side with keys matching the object field-names.

ObjectMapper mapper = new ObjectMapper(); Contact contact = mapper.readValue(json, Contact.class); 

You can use ObjectMapper to deserialize - piece of cake ;)

Sign up to request clarification or add additional context in comments.

3 Comments

Great anwser! Just adding that, in my case, i need to put jackson-mapper-asl-1.9.2.jar in classpath.
OMG... I love you. Spent ages messing with gson. Never realised it was this simple all along!
@Topera - additionally we need the jackson-core-asl jar too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.