Jersey can produce List<T> but cannot Response.ok(List<T>).build()?
Asked Answered
P

3

24

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.

Pennipennie answered 21/5, 2011 at 11:53 Comment(1)
Here is the best solution [enter link description here][1] [1]: #27342288Benil
P
25

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>>() {
    });
}
Pennipennie answered 21/5, 2011 at 12:26 Comment(4)
there is a small catch in this solution is that you need guavaSpoonerism
@Necronet: Guava is not required for the server-side solution provided above, correct?Neuropath
@Neuropath Lists.newArrayList is from Guava. You can easily replace it with new ArrayList and add in pure Java.Pennipennie
Ah yes, I missed that. Good solution either way.Neuropath
U
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.

    @GET
    @Produces(MediaType.APPLICATION_XML)
    public Response getEvents(){
        List<Event> events = eventService.getAll();
        return Response.ok(events.toArray(new Event[events.size()])).build();
    }
Ugly answered 31/8, 2011 at 22:2 Comment(0)
Y
0

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);
    });
}
Yuonneyup answered 20/11, 2016 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.