Java 泽西岛:响应列表<;DBObject>;
我想用Jersey发送Json。 我使用mongoDb 返回我的对象的My函数:Java 泽西岛:响应列表<;DBObject>;,java,json,mongodb,jersey,dbobject,Java,Json,Mongodb,Jersey,Dbobject,我想用Jersey发送Json。 我使用mongoDb 返回我的对象的My函数: public static List<DBObject> getAll(){ List<DBObject> toReturn = new ArrayList<DBObject>(); DBCollection coll = Db.databse.getCollection("Roles"); DBCursor cursor = coll.find();
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;
}
我用邮递员。
邮递员收到200,但不是我的JSON。
如果有人能帮我。
Thx.我找到了一个解决方案:
将dbCollection解析为List的My函数
公共静态列表getAll(){
Gson Gson=新的Gson();
List toReturn=new ArrayList();
DBCollection coll=Db.database.getCollection(“角色”);
DBCursor cursor=coll.find();
试一试{
while(cursor.hasNext()){
System.out.print(cursor.next());
Role r=gson.fromJson(cursor.next().toString(),Role.class);
t返回。添加(r);
}
}最后{
cursor.close();
}
回归回归;
}
将列表返回到json响应的My函数:
@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();
}
@GET
@路径(“/”)
公众回应getAll(){
List roleList=new ArrayList();
roleList=Role.getAll();
字符串json=new Gson().toJson(roleList);
返回Response.status(200).entity(json.build();
}
希望这能帮助这个世界上的某个人
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;
}
@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();
}