Of course you can also use JResponse, the Jersey specific but hopefully
in JAX-RS 2.0, version of Response:
@GET
@Produces(MediaType.APPLICATION_JSON)
public JResponse<List<Bean>> get() {
Stock stock = new Stock();
stock.setQuantity(3);
return
JResponse.<List<Bean>>ok(Lists.newArrayList(stock)).build();
}
This has the advantage of exposing the generic type in javadoc whereas
the GenericEntity solution hides the types internally to the method,
useful for WADL generation when you need the XML Schema generated also.
Gerard
On 24/05/2011 13:25, Martin Matula wrote:
> On 24.5.2011 12:07, Pavel Bucek wrote:
>> Hello,
>>
>> this should work:
>>
>> return Response.ok(
>> new GenericEntity<List<Stock>>(Lists.newArrayList(stock)){}
>> ).build();
>
> You need to use the "workaround" above due to the type erasure
> (http://download.oracle.com/javase/tutorial/java/generics/erasure.html) -
> otherwise Jersey is not able to see the type parameter during the
> runtime.
> Martin
>>
>>
>> Regards,
>> Pavel
>>
>> On 5/21/11 1:59 PM, amsellem.yves wrote:
>>> Jersey 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.
>>>
>>> --
>>> View this message in context:
>>> http://jersey.576304.n2.nabble.com/Jersey-can-produce-List-T-but-cannot-Response-ok-List-T-build-tp6389283p6389283.html
>>> Sent from the Jersey mailing list archive at Nabble.com.
>>>
>>
--
Gerard Davison | Senior Principal Software Engineer | +44 118 924 5095
Oracle JDeveloper Web Service, Spring, Weblogic SCA Tooling Development
Oracle Corporation UK Ltd is a company incorporated in England& Wales.
Company Reg. No. 1782505.
Reg. office: Oracle Parkway, Thames Valley Park, Reading RG6 1RA.
Blog http://kingsfleet.blogspot.com Twitter kingsfleet