如何使用 RestEasy 将 JSON 转换为 POJO 列表 [英] How to convert JSON to list of POJOs using RestEasy

查看:37
本文介绍了如何使用 RestEasy 将 JSON 转换为 POJO 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将我们的 j2ee 应用程序与 REST 网络服务集成.我想使用 JBoss 的 RestEasy JAX-RS 实现.Web 服务以 JSON 格式返回一个数组.我有这段代码:

I have to integrate our j2ee application with a REST webservice. And I wanted to use the RestEasy JAX-RS implementation from JBoss. The webservice returns an array in JSON format. I've this piece of code:

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://myservices.com/schemes/all");
Response response = target.request().get();

我可以使用 RestEasy 将此响应"对象映射到 List 吗?谢谢

Can I map this "response" object to List<Scheme> using RestEasy? Thanks

推荐答案

如果您的 JSON 提供程序能够将 JSON 转换为适当的实体,则可以.您在代码中调用的 get 方法有一个重载版本,它接受要转换结果的实体类.由于序列化某些集合的实现存在问题,因此您的类型必须包含在 GenericType 类中,如下所示:

Provided that your JSON provider is capable of converting JSON to appropriate entities, then yes. The get method you call in the code has an overloaded version which accepts the class of entity to which the result is to be converted. Since there are problems with serializing certain collections' implementations, your type has to be wrapped in GenericType class, like that:

List<Scheme> schema = [...].get(new GenericType<List<Scheme>>(){});

上述方法应该适用于几乎所有符合 JAX-RS 的实现.

The above method should work with just about every JAX-RS-compliant implementation.

您还可以使用 Jackson 库,它允许您(除其他外)传递集合而无需包装它们.

You can also use Jackson library, which allows you (amongst other things) to pass collections without need of wrapping them.

这篇关于如何使用 RestEasy 将 JSON 转换为 POJO 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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