[英]Jersey : response with List<DBObject>
I want to send Json with Jersey. 我想寄给Json和Jersey。 I use mongoDb. 我使用mongoDb。
My function to return my objects : 我的功能返回我的对象:
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;
}
And my jersey method to return json : 而我的球衣方法返回json:
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response getAll(){
return Response.status(200).entity(Role.getAll()).build();
}
I use POSTMAN. 我使用POSTMAN。 POSTMAN receives 200 but not my JSON. POSTMAN收到200,但没有收到我的JSON。 If somebody can help me. 如果有人可以帮助我。 Thx. 谢谢。
I find a solution : My function to parse dbCollection to List<> 我找到一个解决方案:我的函数将dbCollection解析为List <>
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;
}
My function to return List<> to json response : 我将List <>返回到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();
}
Hope that will help somebody in this world. 希望能对这个世界有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.