dev@jsr311.java.net

Re: JSR311: TypeLiteral

From: Stephan Koops <Stephan.Koops_at_web.de>
Date: Wed, 04 Jun 2008 10:58:45 +0200

Hi,

what about a generic Response class?
Let's stay at the given example:
public List<Bean> getList() { ... }
could become
public Response<List<Bean>> getList getList() { ... }

best regards
   Stephan

Marc Hadley schrieb:
> Type erasure creates an issue for resource method return values. E.g.
> given:
>
> @GET
> List<Bean> getList() {
> List<Bean> results = ...;
> return results;
> }
>
> we can determine the generic return type and find a suitable message
> body writer. However, if the method needs to add additional metadata
> then it has to return an instance of Response like this:
>
> @GET
> Response getList() {
> List<Bean> results = ...;
> return Response.ok().entity(results).cacheControl(...).build();
> }
>
> The problem is that because of type erasure we lose the type of list
> that forms the entity. To get round this we'd like to add new methods
> to ResponseBuilder that include a parameter that provides the missing
> information and add a corresponding method to Response to extract that
> information.
>
> Google Guice includes a TypeLiteral[1] class that seems to offer
> everything we need and Bob Lee (Google) has kindly agreed to donate it
> for inclusion in this JSR. Usage would look something like this:
>
> @GET
> Response getList() {
> List<Bean> results = ...;
> TypeLiteral<List<Bean>> listOfBean = new TypeLiteral<List<Bean>>() {};
> return
> Response.ok().entity(results).genericType(listOfBean).cacheControl(...).build();
>
> }
> Thoughts, comments ?
>
> Marc.
>
> [1]
> http://google-guice.googlecode.com/svn/trunk/src/com/google/inject/TypeLiteral.java
>