[英]Why I am not getting exact data when I am using rest api with MongoDB?
I am getting following data when I am using MongoDB Rest API. 当我使用MongoDB Rest API时,我得到了以下数据。
Cursor id=0, ns=test.products, query={ }, numIterated=1, addr=localhost/127.0.0.1:27017, readPreference=primary
I am expecting JSON data, but I am not getting that data. 我期待JSON数据,但我没有得到这些数据。 For this i am maintaining following URL 为此我保持以下URL
localhost:8080/API/rest/applicationInitiatorService/addContact
Please find the below Code 请查看以下代码
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();
}}
And find Respected method where I am calling a method. 并找到我调用方法的尊重方法。
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;
}
This data is comming your REST api, please check the api code and see whats happening there. 这个数据是你的REST api,请检查api代码,看看那里发生了什么。 My guess is you are returning the incorrect variable. 我的猜测是你正在返回不正确的变量。
Could you include the code from your API call ? 你可以在API调用中加入代码吗?
UPDATE: I would suggest you change: 更新:我建议你改变:
String addrecord=cursor.toString();
to: 至:
String addrecord=cursor.next().toString();
I think probably something to do with cursor or toString. 我想可能与游标或toString有关。 Could you try converting the cursor data to a collection and return it? 您可以尝试将游标数据转换为集合并返回它吗?
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();
}
Something like 就像是
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();
}
}
or you can try the return statement as 或者您可以尝试将return语句作为
return Response.status(200).entity(new Gson().toJson(cursor.toArray())).build();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.