javax.ws.rs.ProcessingException:RESTEASY003145:无法找到内容类型为text / plain的MessageBodyReader并键入interface java.util.List

时间:2017-07-09 18:30:56

标签: java web-services request marshalling

我试图将包含通道列表的响应存储到客户端的列表对象中,以便它可以在JSP中显示其内容。

现在,我将列表包装到GenericEntity中,因为我知道它只会以这种方式编译。

更新:我使用REST 3.1.3-Final-all,我还在其中添加了所有JAR。从研究来看,我认为原因是我没有杰克逊提供者罐子,但这还没有解决问题。

我也将列表包装在另一方的GenericType中。

我对于什么是" MessageBodyReader"而感到困惑。是的,因为我得到了一个显示以下内容的例外:

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)

我尝试测试的代码段如下:

客户端(在离线主类中测试):

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.");

        }
    }
}

和RESTFUL Service类的方法:

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();

    }
}

有几点需要注意:

  1. 我没有使用Maven,因为我只想在MediaType.TEXT_PLAIN中显示每个,但是如果它更容易切换到APPLICATION_XML或APPLICATION_JSON,那么请告诉我。

    < / LI>
  2. 我的目的是简单地将列表Response转换为javatype列表,以便稍后我可以在JSP中的表中显示它的每个元素。

  3. 如果有人可以向我解释一下MessageBodyReader是什么,我将不胜感激。

0 个答案:

没有答案