Deserialize JSON to Object using Jersey

柔情痞子 提交于 2019-12-10 05:48:04

问题


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:


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 ;)



来源:https://stackoverflow.com/questions/11831981/deserialize-json-to-object-using-jersey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!