I have a MessageBodyReader
with the following class:
@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class TransactionMessageBodyReader implements MessageBodyReader<Transaction<Customer>>
boolean isReadable(Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType)
And I have the following REST endpoint:
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("trans")
public Response checkStatus(Transaction<Customer> transaction) { ... }
In my debugger I have a breakpoint on isReadable
and it hits the breakpoint before calling the checkStatus
method. Good so far. I can see in my debugger that the genericType
says Transaction<Customer>
... in other words, it knows about the type information. How can it possibly know the type information when Java type erasure erases it to just be Transaction
at runtime? I see that the genericType
is made up of an ParameterizedType
which I believe is used to pass type information around (to get around the type erasure problem). However, how is Jersey populating the genericType
automatically? Because I never specified type information except in the method headers that I posted above.
Also, a second question: if Jersey 'automatically' knows so much about my types, why do I need a MessageBodyReader
at all? Isn't there some easier way to use generics with Jersey/REST endpoints and make it "just work"?