[英]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.