简体   繁体   English

当我在MongoDB上使用rest api时,为什么我没有得到确切的数据?

[英]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();

For this you will need to use Gson library. 为此,您需要使用Gson库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 刷新数据时发生异常。 这是什么,为什么我得到这个? - Exception occurred when flushing data . What is this and why am I getting this? 为什么即使使用hasNextInt()方法,我也会得到inputMismatchException? - Why am I getting inputMismatchException even when using hasNextInt() method? 使用SAXParser时为什么会出现“MalformedURLException:no protocol”? - Why am I getting “MalformedURLException: no protocol” when using SAXParser? 为什么我在使用 Kryo 时会收到此 NullPointerException? - Why am I getting this NullPointerException when using Kryo? 为什么将add()和getFirst()用于LinkedList时会出现此错误? - Why am I getting this error when using add() and getFirst() for a LinkedList? 为什么在 JAVA 中使用扫描仪时出现运行时错误 - Why I am getting a RUNTIME ERROR when using Scanner in JAVA 为什么我在解密时收到“BadPaddingException”? - Why am I getting 'BadPaddingException' when decrypting? 为什么我在Jersey REST API上的POST收到500响应? - Why am I getting a 500 response for my POST on my Jersey REST API? 为什么当我有 2 个具有相同端点字符串值的 controller 方法时出现“requestMappingHandlerMapping”错误 - Why am I getting a "requestMappingHandlerMapping" error when I have 2 controller methods which has same exact endpoint string value 当我从html表单传递值时,为什么在我的post api期间出现'Column not not null'的错误 - Why am I getting an error for 'Column cannot be null' during my post api when I am passing a value from a html form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM