当我使用MongoDB Rest API时,我得到了以下数据。
Cursor id=0, ns=test.products, query={ }, numIterated=1, addr=localhost/127.0.0.1:27017, readPreference=primary
我期待JSON数据,但我没有得到这些数据。 为此,我正在维护以下网址
localhost:8080/API/rest/applicationInitiatorService/addContact
请找到以下代码
import javax.ws.rs.*;
@Path("/applicationInitiatorService/")
public class ApplicationInitiatorService {
@GET
@Path("/addContact")
@Produces("application/json")
public Response add_Contact() {
DBCursor cursor=null;
cursor=ApplicationInitiatorFactory.getApplicationinitiator().add_record(getcCollection);
String addrecord=cursor.toString();
return Response.status(200).entity(addrecord).build();
}}
找到我称之为方法的尊重方法。
public DBCursor add_record(DBCollection db_collection) {
BasicDBObject document = new BasicDBObject();
document.put("database", "Bosch_DB");
document.put("table", "generating");
BasicDBObject documentDetail = new BasicDBObject();
documentDetail.put("total_phone_no", 100);
documentDetail.put("vps_index", "vps_index1");
documentDetail.put("active", "true");
document.put("detail", documentDetail);
db_collection.insert(document);
DBCursor cursorDoc = db_collection.find();
while (cursorDoc.hasNext()) {
System.out.println(cursorDoc.next());
}
return cursorDoc;
}
答案 0 :(得分:0)
这个数据是你的REST api,请检查api代码,看看那里发生了什么。我的猜测是你要返回不正确的变量。
您可以在API调用中添加代码吗?
更新: 我建议你改变:
String addrecord=cursor.toString();
为:
String addrecord=cursor.next().toString();
答案 1 :(得分:0)
我认为可能与游标或toString有关。您可以尝试将游标数据转换为集合并返回它吗?
public DBCursor add_record(DBCollection db_collection) {
BasicDBObject document = new BasicDBObject();
document.put("database", "Bosch_DB");
document.put("table", "generating");
BasicDBObject documentDetail = new BasicDBObject();
documentDetail.put("total_phone_no", 100);
documentDetail.put("vps_index", "vps_index1");
documentDetail.put("active", "true");
document.put("detail", documentDetail);
db_collection.insert(document);
return db_collection.find();
}
像
这样的东西import javax.ws.rs.*;
@Path("/applicationInitiatorService/")
public class ApplicationInitiatorService {
@GET
@Path("/addContact")
@Produces("application/json")
public Response add_Contact() {
DBCursor cursor = ApplicationInitiatorFactory.getApplicationinitiator().add_record(getcCollection);
return Response.status(200).entity(cursor.toArray()).build();
}
}
或者你可以尝试将return语句作为
return Response.status(200).entity(new Gson().toJson(cursor.toArray())).build();
为此,您需要使用Gson库。