Jersey 可以生产 List但不能 Response.ok(List).build() 吗?
Jersey 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();
}
}
给出错误: Java 类 java.util.ArrayList 和 Java 类型类 java.util.ArrayList 和 MIME 媒体类型 application/json 的消息正文编写器为not find
这会阻止使用 HTTP 状态代码和标头。
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);
}
}
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
This prevent the use of HTTP status code and headers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以通过以下方式在响应中嵌入
List
:客户端必须使用以下行来获取
List
:It is possible to embed a
List<T>
in a Response the following way:The client have to use the following lines to get the
List<T>
:由于某种原因,GenericType 修复对我不起作用。然而,由于类型擦除是针对集合完成的,而不是针对数组,所以这是有效的。
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.
我对使用 AsyncResponse 的方法的解决方案
my solution for methods that use AsyncResponse