如何将列表<string>作为来自jersey2客户端的响应

时间:2016-02-10 11:20:47

标签: java json jersey-2.0 jersey-client

我试过了,

@event = EventsController.new request

@event.check    #-> "busy_array" locally scoped to this function
@event.response #-> outputs view code.... @busy_array needs to be instance var

但是我没有得到列表而是获得空值

4 个答案:

答案 0 :(得分:36)

在响应对象中获取响应,然后使用readEntity()方法解析响应对象。

以下是快速代码段:

Response serviceResponse = client.target(url).
                    request(MediaType.APPLICATION_JSON).get(Response.class);
List<String> list = serviceResponse.readEntity(new GenericType<List<String>>() {
                });

答案 1 :(得分:0)

String listString= serviceResponse.readEntity(String.class);
Gson gson=new Gson();
Type type = new TypeToken<List<String>>(){}.getType();
List<String> list = gson.fromJson(listString, type);

获取响应字符串,然后使用gson库

转换为List

答案 2 :(得分:0)

首先添加杰克逊依赖项

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.27</version>
</dependency>

然后创建客户端配置

ClientConfig config = new ClientConfig();
config.register( JacksonFeature.class );

最后通过ClientConfig创建客户端

List<String> list = ClientBuilder.newClient( config )
               .target( uri )
               .request()
               .get( Response.class )
               .readEntity( List.class );

答案 3 :(得分:-2)

1)在你的Response中使用readEntity()方法解析响应对象。

List<String> list = client.target(url).
request(MediaType.APPLICATION_JSON).get(Response.class).readEntity(new GenericType<List<String>>() {
});
相关问题