泽西岛可以生产List< T>。但不能Response.ok(List< T>)。build()? [英] Jersey can produce List<T> but cannot Response.ok(List<T>).build()?

查看:143
本文介绍了泽西岛可以生产List< T>。但不能Response.ok(List< T>)。build()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

泽西岛1.6可以产生:

Jersey 1.6 can produce:

@Path("/stock")
public class StockResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Stock> get() {
        Stock stock = new Stock();
        stock.setQuantity(3);
        return Lists.newArrayList(stock);
    }
}

但不能用同样的方法:

@Path("/stock")
public class StockResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response get() {
        Stock stock = new Stock();
        stock.setQuantity(3);
        return Response.ok(Lists.newArrayList(stock)).build();
    }
}

给出错误: A用于Java类java.util.ArrayList的消息体编写器和Java类型类java.util.ArrayList,未找到MIME媒体类型application / json

这可以防止使用HTTP状态代码和标题。

This prevent the use of HTTP status code and headers.

推荐答案

可以嵌入以下列方式在响应中列出< T>

@Path("/stock")
public class StockResource {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response get() {
        Stock stock = new Stock();
        stock.setQuantity(3);

        GenericEntity<List<Stock>> entity = 
            new GenericEntity<List<Stock>>(Lists.newArrayList(stock)) {};
        return Response.ok(entity).build();
    }
}

客户必须使用以下行来获取列表< T>

The client have to use the following lines to get the List<T>:

public List<Stock> getStockList() {
    WebResource resource = Client.create().resource(server.uri());
    ClientResponse clientResponse =
        resource.path("stock")
        .type(MediaType.APPLICATION_JSON)
        .get(ClientResponse.class);
    return clientResponse.getEntity(new GenericType<List<Stock>>() {
    });
}

这篇关于泽西岛可以生产List&lt; T&gt;。但不能Response.ok(List&lt; T&gt;)。build()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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