Jersey 1.6 can produce:
泽西岛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);
}
}
But cannot do the same with:
但不能这样做:
@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();
}
}
Giving the error: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/json was not found
给出错误:Java类Java .util的消息体编写器。ArrayList和Java类Java .util。没有找到ArrayList和MIME媒体类型应用程序/json。
This prevent the use of HTTP status code and headers.
这防止了HTTP状态码和头信息的使用。
3 个解决方案
#1
24
It is possible to embed a List<T>
in a Response the following way:
可以在响应中嵌入一个列表
@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();
}
}
The client have to use the following lines to get the List<T>
:
客户端必须使用以下行来获取List
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>>() {
});
}
#2
8
For some reason the GenericType fix wasn't working from me. However, since type erasure is done for Collections but not for Arrays, this worked.
由于某种原因,GenericType修复程序并没有从我这里工作。但是,由于类型擦除是为集合而不是为数组做的,所以这是有效的。
@GET
@Produces(MediaType.APPLICATION_XML)
public Response getEvents(){
List<Event> events = eventService.getAll();
return Response.ok(events.toArray(new Event[events.size()])).build();
}
#3
0
my solution for methods that use AsyncResponse
我对使用AsyncResponse方法的解决方案。
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void list(@Suspended
final AsyncResponse asyncResponse) {
asyncResponse.setTimeout(10, TimeUnit.SECONDS);
executorService.submit(() -> {
List<Product> res = super.listProducts();
Product[] arr = res.toArray(new Product[res.size()]);
asyncResponse.resume(arr);
});
}
#1
24
It is possible to embed a List<T>
in a Response the following way:
可以在响应中嵌入一个列表
@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();
}
}
The client have to use the following lines to get the List<T>
:
客户端必须使用以下行来获取List
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>>() {
});
}
#2
8
For some reason the GenericType fix wasn't working from me. However, since type erasure is done for Collections but not for Arrays, this worked.
由于某种原因,GenericType修复程序并没有从我这里工作。但是,由于类型擦除是为集合而不是为数组做的,所以这是有效的。
@GET
@Produces(MediaType.APPLICATION_XML)
public Response getEvents(){
List<Event> events = eventService.getAll();
return Response.ok(events.toArray(new Event[events.size()])).build();
}
#3
0
my solution for methods that use AsyncResponse
我对使用AsyncResponse方法的解决方案。
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void list(@Suspended
final AsyncResponse asyncResponse) {
asyncResponse.setTimeout(10, TimeUnit.SECONDS);
executorService.submit(() -> {
List<Product> res = super.listProducts();
Product[] arr = res.toArray(new Product[res.size()]);
asyncResponse.resume(arr);
});
}