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!
Hi,
I'm trying to read a simple list of String, but I cannot due to the error: "No message body reader has been found for class java.util.List".
Please, could you help me?
Thanks in advance,
Antonio.
Posted by Antonio on October 31, 2017 at 10:55 AM CET #
Thanks a lot definitely this helped me i had this same issue . The funny thing is that i could use the same Rest client on a normal Java application but had this issue while using Rest client on JSF controller class.
Regards
Posted by miguel on June 14, 2018 at 02:15 AM CEST #