I'm trying to store a response comprising of a list of channels into a List Object in the client, so that it can display its contents in a JSP.
Right now, I wrapped the list into a GenericEntity, because I know it will only compile this way.
UPDATE: I'm using REST 3.1.3-Final-all, and I have also added all the JARs in them. From research, I thought the reason was that I don't have the jackson provider jars, but this has not solved the problem.
I also am wrapping the list in a GenericType from the other side.
I'm confused as to what a "MessageBodyReader" is, as I'm getting an exception that shows the following:
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Client created.
Exception in thread "main" javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type text/plain and type interface java.util.List
at org.jboss.resteasy.core.interception.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:42)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:75)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:52)
at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:55)
at org.jboss.resteasy.security.doseta.DigitalVerificationInterceptor.aroundReadFrom(DigitalVerificationInterceptor.java:36)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:55)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:251)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:181)
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:225)
at examples.pubhub.utilities.test.main(test.java:33)
The segment of code I'm trying to test is as follows:
client (tested in an offline main class):
package examples.pubhub.utilities;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.SyncInvoker;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import examples.pubhub.model.Channel;
public class test {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
System.out.println("Client created.");
WebTarget target = client.target("http://localhost:8080/RESTService/service/authorchannels/channels/");
Response response = target.request().accept(MediaType.TEXT_PLAIN).get();
List<Channel> channels = response.readEntity(new GenericType<List<Channel>>() {});
if (response.getStatus() != 200) {
System.out.println("ChannelDAOImpl utilized, but channels could not be retrieved!" );
} else {
System.out.println(response.getMediaType().toString());
System.out.println(response.readEntity(String.class));
System.out.println("Channels? " + channels);
System.out.println("ChannelDAOImpl utilized; channels succesfully acquired.");
}
}
}
and the RESTFUL Service class with method:
package methods;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import model.Channel;
import resources.DAOUtilities;
@Path("/authorchannels")
public class AuthorChannel {
@SuppressWarnings("unchecked")
@GET
@Path("/channels")
public Response getAllChannels() {
System.out.println("Accessing RESTService, and acquiring channels...");
SessionFactory sessionFactory = DAOUtilities.getSessionFactory();
System.out.println("Session Factory acquired.");
Session session = sessionFactory.openSession();
System.out.println("Session opened.");
session.beginTransaction();
Query query = session.createQuery("from Channel");
List<Channel> channels = (List<Channel>) query.list();
GenericEntity<List<Channel>> list = new GenericEntity<List<Channel>>(channels) {};
System.out.println("Did we get the channels? " + channels);
System.out.println("Channel users? " + ((List<Channel>) channels).get(0).getChannel_user());
System.out.println("Channel bios? " + ((List<Channel>) channels).get(0).getChannel_bio());
session.getTransaction().commit();
//prepare response status to be returned to client
ResponseBuilder respBuilder = null;
if (channels != null)
respBuilder = Response.status(Response.Status.OK);
else
respBuilder = Response.status(Response.Status.NOT_FOUND);
//deliver response to client, indicating that the channels have been acquired.
return respBuilder.status(200).entity(list).build();
}
}
A few things to note:
I'm not using Maven, as I only want to display each one in MediaType.TEXT_PLAIN, but if its easier to switch to APPLICATION_XML or APPLICATION_JSON, then please let me know.
My intention is to simply convert the list Response into a javatype list, so that I can display each of its elements in a table in a JSP later on.
If someone can explain to me what a MessageBodyReader is, I would greatly appreciate it.