adam bien's blog

JAX-RS 2.0 Client: Retrieving A List Of Instances, Problem and Solution 📎

Retrieving a generic List in a JAX-RS 2.0 client:

List<Workshop> all = this.contextURI.request(MediaType.APPLICATION_XML).get(List.class);

Does not carry sufficient amount of information to deserialize the payload and leads to following (or similar exception):

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: 
MessageBodyReader not found for media type=application/xml, type=interface java.util.List, genericType=interface java.util.List.
	at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:230)
	at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:154)
	at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
	at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)
	at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:783)


The javax.ws.rs.core.GenericType helper solves the problem by wrapping the List and providing the necessary information to the built-in deserializer (MessageBodyReader):

import javax.ws.rs.core.GenericType;

List<Workshop> all = target.request(MediaType.APPLICATION_XML).get(new GenericType<List<Workshop>>() {});

There is a symmetric challenge on the server counterpart sending the list to the client.

See you at Java EE Workshops at Munich Airport, Terminal 2 particularly at Effective Java EE!