2014-09-01 5 views
1

Я хочу отправить Json с Джерси. Я использую mongoDb.Джерси: ответ со списком <DBObject>

Моя функция вернуть мои объекты:

public static List<DBObject> getAll(){ 
    List<DBObject> toReturn = new ArrayList<DBObject>(); 
    DBCollection coll = Db.databse.getCollection("Roles"); 
    DBCursor cursor = coll.find(); 
    try { 
     while(cursor.hasNext()) { 
      toReturn.add(cursor.next()); 
     } 
    } finally { 
     cursor.close(); 
    } 

    return toReturn; 
} 

И мой метод Джерси вернуть JSON:

@GET 
@Path("/") 
@Produces(MediaType.APPLICATION_JSON) 
public Response getAll(){ 
     return Response.status(200).entity(Role.getAll()).build(); 
} 

Я использую почтальона. POSTMAN получает 200, но не мой JSON. Если кто-то может мне помочь. Thx.

ответ

2

я найти решение: Моя функция для разбора dbCollection к списку <>

public static List<Role> getAll(){ 
    Gson gson = new Gson(); 
    List<Role> toReturn = new ArrayList<Role>(); 
    DBCollection coll = Db.databse.getCollection("Roles"); 
    DBCursor cursor = coll.find(); 
    try { 
     while(cursor.hasNext()) { 
      System.out.print(cursor.next()); 
      Role r = gson.fromJson(cursor.next().toString(),Role.class); 
      toReturn.add(r); 
     } 
    } finally { 
     cursor.close(); 
    } 
    return toReturn; 
} 

Моя функция возвращала Список <> в ответ JSon:

@GET 
@Path("/") 
public Response getAll(){ 
    List<Role> roleList = new ArrayList<Role>(); 
    roleList = Role.getAll(); 
    String json = new Gson().toJson(roleList); 
    return Response.status(200).entity(json).build(); 
} 

Надежда, которая поможет кому-то в этот мир.