0

My query returns Iterable elements and now I want to return it to the user in JSON format but Jersey can not convert it. it says:

A message body writer for Java class org.jongo.MongoIterator, and Java type java.lang.Iterable, and MIME media type application/xml was not found

    @GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Iterable<Complex> getISS(){
    DB db = ConnectionHelper.client.getDB("testdb");
    Jongo jongo = new Jongo(db);
    MongoCollection complex = jongo.getCollection("COMPLEX");
    Iterable<Complex> all = complex.find().as(Complex.class);       
    return all;
}

Do I need to convert it to List type or there is other effective way to do it?

Abzal Kalimbetov
  • 505
  • 7
  • 20

1 Answers1

3

You can use newArrayList method from Guava library

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Iterable<Complex> getISS(){
    DB db = ConnectionHelper.client.getDB("testdb");
    Jongo jongo = new Jongo(db);
    MongoCollection complex = jongo.getCollection("COMPLEX");
    Iterable<Complex> all = complex.find().as(Complex.class);

    return Lists.newArrayList(all);
}

or copy iterable into a new List, see https://stackoverflow.com/a/10117051/122975 for more information

BTW you should no instanciate Jongo each time you handle a new request.

Community
  • 1
  • 1
Benoît Guérout
  • 1,977
  • 3
  • 21
  • 30