使用Jersey将反序列化JSON到对象反序列化 [英] Deserialize JSON to Object using Jersey

查看:211
本文介绍了使用Jersey将反序列化JSON到对象反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Jersey,它使用客户端的JSON或xml创建对象.

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

现在,我正在使用JSONp创建对象.由于jsonp无法发表信息,因此我只能使用get发送数据.

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

  1. 我可以使用GET请求并且仍然反序列化吗?我知道我可以为每个使用@QueryParam,然后以某种方式反序列化.但是像POST和Put一样,我可以给它一个输入参数,并希望Jersey自动进行反序列化.

  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.

如何使用Jersey将一个json序列化为Java对象?

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

谢谢.

推荐答案

经过一些研究,直接从json反序列化并不困难.这就是我的方法.

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

使用与对象字段名称匹配的键从客户端发送JSON作为queryparam.

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

您可以使用ObjectMapper对小菜一碟进行反序列化;)

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

这篇关于使用Jersey将反序列化JSON到对象反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆