如何使用RestEasy将JSON转换为POJO列表 [英] How to convert JSON to list of POJOs using RestEasy
问题描述
我必须将我们的j2ee应用程序与REST Web服务集成。我想使用JBoss的RestEasy JAX-RS实现。 webservice以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();
我可以将此响应对象映射到列表< Scheme>
使用RestEasy?谢谢
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
-compliant implementation。
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屋!