Jersey 可以生产 List但不能 Response.ok(List).build() 吗?

发布于 2024-11-08 16:43:37 字数 763 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰◇生如夏花灿烂 2024-11-15 16:43:37

可以通过以下方式在响应中嵌入 List

@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

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>>() {
    });
}

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>:

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>>() {
    });
}
嗫嚅 2024-11-15 16:43:37

由于某种原因,GenericType 修复对我不起作用。然而,由于类型擦除是针对集合完成的,而不是针对数组,所以这是有效的。

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response getEvents(){
        List<Event> events = eventService.getAll();
        return Response.ok(events.toArray(new Event[events.size()])).build();
    }

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.

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response getEvents(){
        List<Event> events = eventService.getAll();
        return Response.ok(events.toArray(new Event[events.size()])).build();
    }
擦肩而过的背影 2024-11-15 16:43:37

我对使用 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);
    });
}

my solution for methods that use 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);
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文