2014-09-01 1 views
1

Je veux envoyer Json avec Jersey. J'utilise mongoDb.Jersey: réponse avec la liste <DBObject>

Ma fonction pour retourner mes objets:

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; 
} 

Et ma méthode jersey pour revenir JSON:

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

J'utilise POSTMAN. POSTMAN reçoit 200 mais pas mon JSON. Si quelqu'un peut m'aider. Thx.

Répondre

2

je trouve une solution: Ma fonction parse dbCollection Liste <>

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; 
} 

Ma fonction pour revenir Liste <> pour réponse 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(); 
} 

Espoir qui aidera quelqu'un ce monde.

Questions connexes