How to convert JSON to list of POJOs using RestEasy

淺唱寂寞╮ 提交于 2019-12-20 04:53:47

问题


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

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


回答1:


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>>(){});

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

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



来源:https://stackoverflow.com/questions/32634530/how-to-convert-json-to-list-of-pojos-using-resteasy

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