可以List<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(); }}
客户必须使用以下行来获取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>>() { });}解决方法
球衣1.6可以产生:
@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 message body writer for Java class java.util.ArrayList,and Java typeclass java.util.ArrayList,and MIME media type application/json was not found
这样可以防止使用HTTP状态代码和标头。