On Aug 7, 2009, at 11:13 AM, Ari Heino wrote:
>>
> Is it possible to return those collections in a response instead of
> returning the collection itself as a return value of the method? To
> make use
> of Response's status codes and such?
>
> So instead of:
>
> @GET
> public List<String> getStuph(){
> return new ArrayList();
> }
>
> I could have:
>
> @GET
> public Response getStuph() {
> return Response.ok().entity(new ArrayList()).build();
> }
>
> I tried this with Jersey 1.0.3. and it complains:
>
> 7.8.2009 10:52:49 com.sun.jersey.spi.container.ContainerResponse write
> SEVERE: A message body writer for Java type, class
> java.util.ArrayList, and
> MIME media type, text/xml, was not found
>
> I tried using @Produces( { MediaType.APPLICATION_XML }) and
> @Produces( {
> MediaType.TEXT_XML }) with no difference to the error result.
>
You need to use GenericEntity to preserve the generic type information.
https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/core/GenericEntity.html
List<String> list = new ArrayList<String>();
GenericEntity<List<String>> entity = new
GenericEntity<List<String>>(list) {};
Response response = Response.ok(entity).build();
If you use arrays you do not need to utilize GenericEntity.
Unfortunately this is the only way to do things because of type erasure.
Paul.