简体   繁体   English

未找到 Media type=text/plain、type=class java.util.ArrayList、genericType=java.util.List 的 MessageBodyWriter<models.Person>

[英]MessageBodyWriter not found for media type=text/plain, type=class java.util.ArrayList, genericType=java.util.List<models.Person>

I have written a jersey REST API method which returns a list of persons which is queried from mysql backend using hibernate.我编写了一个 jersey REST API 方法,该方法返回使用 hibernate 从 mysql 后端查询的人员列表。 Here is the method这是方法

    @Path("Person")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public List<Person> person()
    {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        Criteria cr = session.createCriteria(Person.class);
        List persons =  cr.list();
        GenericEntity<List<Person>> list = new GenericEntity<List<Person>>(persons) {};
        tx.commit();
        session.close();
        return persons;

    }

and here is my MessageBodyWriter这是我的MessageBodyWriter

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class PersonMessageBodyWriter implements MessageBodyWriter<Person> {

@Override
public long getSize(Person arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
    // TODO Auto-generated method stub
    return -1;
}

@Override
public boolean isWriteable(Class<?> type, Type arg1, Annotation[] arg2, MediaType arg3) {
    return Person.class.isAssignableFrom(type);
}

@Override
public void writeTo(Person person, Class<?> type, Type type1, Annotation[] arg3, MediaType arg4,
        MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
    // TODO Auto-generated method stub
        out.write(person.toString().getBytes());
    }

}

When i make a get request to the above method i am getting SEVERE: MessageBodyWriter not found for media type=text/plain, type=class java.util.ArrayList, genericType=java.util.List.当我向上述方法发出获取请求时,我收到了严重的问题:找不到 MessageBodyWriter for media type=text/plain, type=class java.util.ArrayList, genericType=java.util.List。 can someone please help me get through with this problem?有人可以帮我解决这个问题吗?

What I've always done in this case is make a Persons object that contains the List of persons.在这种情况下,我一直在做的是创建一个包含人员列表的 Persons 对象。 There may be a better way but if your under a time crunch and want to get it done then this should work.可能有更好的方法,但如果您时间紧迫并想完成它,那么这应该可行。 If it doesn't work then my guess that no JSON marshalling is working, in which case you have a configuration problem.如果它不起作用,那么我猜测没有 JSON 编组工作,在这种情况下,您有配置问题。

import java.util.ArrayList;
import java.util.List;

public class Persons
{
    private List<Person> persons = new ArrayList<>();

    public List<Person> getPersons()
    {
        return persons;
    }

    public void setPersons(List<Person> persons)
    {
        this.persons = persons;
    }
}

I understand that this is quite an old question - However, there is an excellent solution to this now.我知道这是一个很老的问题 - 但是,现在有一个很好的解决方案。 Whenever you wish to return a list of objects, you can utilise the below code:每当您希望返回对象列表时,都可以使用以下代码:

List<KaggleFileResponse> data = kaggleService.listFiles(xxxx);
GenericEntity<List<KaggleFileResponse>> entity = new GenericEntity<List<KaggleFileResponse>>(data) {};
return Response.status(HttpStatus.SC_OK).entity(entity).build();

The code converts the list into a generic entity that can be written back.该代码将列表转换为可写回的通用实体。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到媒体类型=应用程序/json,类型=类 java.util.ArrayList 的 MessageBodyWriter - MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList 找不到响应 object 类型的 MessageBodyWriter:java.util.ArrayList 媒体类型:text/html - 在 Resteasy 中 - Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: text/html - in Resteasy 未找到Java类java.util.ArrayList ...和MIME媒体类型text / xml的消息正文编写器 - A message body writer for Java class java.util.ArrayList…and MIME media type text/xml was not found RestEasy:找不到类型的响应对象的MessageBodyWriter:java.util.Array媒体类型列表:application / json - RestEasy: Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/json 找不到Java类java.util.List和Java类型java.util.List &lt;&gt;和MIME媒体类型text / html的消息正文阅读器 - A message body reader for Java class java.util.List, and Java type java.util.List<>, and MIME media type text/html was not found 找不到Java类型类java.util.ArrayList和MIME媒体类型application / xml的消息正文编写器 - A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found 找不到Java类java.util.ArrayList和Java类型类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器 - A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/json was not found 严重:未找到 Java 类 java.util.ArrayList 和 MIME 媒体类型 application/json 的消息正文编写器 - SEVERE: A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found 找不到Java类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器 - A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found 未找到类型为 java.util.ArrayList 的返回值的转换器 - No converter found for return value of type: class java.util.ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM