-1

This is my first java class

    try {           

        //Build the query
        BasicDBObject query = new BasicDBObject();                                              
        query.put("building_Id", building_Id);
        query.put("floor_Id", floor_Id);

        DBObject removeIdProjection = new BasicDBObject("_id", 0);

        //Provide the query as an argument to the find()
        DBCursor cursor = col.find(query, removeIdProjection);          

        //Iterate the cursor
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

        return new Status("success", cursor.toString());        

    } catch(Exception e) {
        e.printStackTrace();
    }
    return null;                

}

Output for first java class is

{"building_Id" : "3" , "floor_Id" : "23" , "x" : "717.5984497070312" , "y" : "512.9695434570312"}
{"building_Id" : "3" , "floor_Id" : "23" , "x" : "717.5984497070312" , "y" : "514.87548828125"}
{"building_Id" : "3" , "floor_Id" : "23" , "x" : "717.5984497070312" , "y" : "515.8284912109375"}
{"building_Id" : "3" , "floor_Id" : "23" , "x" : "717.5984497070312" , "y" : "517.7344360351562"}

I need to return the above output to second class.

But i`m getting only below output in second java class.

fetch paths response : {"code":"success","message":"DBCursor{collection=DBCollection{database=DB{name='trackbitDB'}, name='objects_path'}, find=FindOptions{, batchSize=0, limit=0, modifiers=null, projection=null, maxTimeMS=0, skip=0, sort=null, cursorType=NonTailable, noCursorTimeout=false, oplogReplay=false, partial=false}, cursor=null}"}

Second java class

    try {

        // api path to list all paths based on floor and building  //fetchpath
        String uri = API_SERVER_PATH + "object/fetchpath?building_Id=" + building_Id + "&floor_Id=" + floor_Id;

        String result = ApiRequestHandler.sendGet(uri);
        System.out.println("fetch paths response : " + result);

        return result;  
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

I think i`m made mistakes while returning. Anybody help me to solve this issue.!

Thanks in advance.!

BMAM
  • 73
  • 1
  • 2
  • 9
  • 1
    You've given us way too much code, and I don't even see a problem statement. What is the actual problem? – Tim Biegeleisen Jan 31 '18 at 06:27
  • Can you specify your problem here? I didn't get what you asked here. And provide necessary code. Your code couldn't be matched to your question. – sbsatter Jan 31 '18 at 06:33
  • The actual problem is, i need to send the data from first java file to second java class. In first java file i`m fetching the data from MongoDB, in second java file i need to get the fetched MongoDB output. After getting the first java output in second java file, i have to pass the values to FrontEnd – BMAM Jan 31 '18 at 06:34
  • We don't even know what are the names of your classes, nor names of methods you're calling. We can guess, but it would be much better if you could provide all necessary info. – Egan Wolf Jan 31 '18 at 06:41
  • You want to get the same result calling a REST API as you get using the mongodb API directly, correct? – Daniel Jan 31 '18 at 06:47
  • Your method should have an approriate return value with the status code an a list with mongo results. Perhaps the jaxrs Result class is the right one? Have a look at https://stackoverflow.com/a/4687942/7298431 – Daniel Jan 31 '18 at 07:39

1 Answers1

-1

return new Status("success", cursor.toString());

You are returning cursor.toString() which is cursor's string representation, which doesn't read content of cursor rather use cursor.next() as you have used in loop.

Create list of status and add status into this list one by one and return list, please see below code:

try {           

    //Build the query
    BasicDBObject query = new BasicDBObject();                                              
    query.put("building_Id", building_Id);
    query.put("floor_Id", floor_Id);

    DBObject removeIdProjection = new BasicDBObject("_id", 0);

    //Provide the query as an argument to the find()
    DBCursor cursor = col.find(query, removeIdProjection);          
    List<Status> statusList = new ArrayList<>(); 
    //Iterate the cursor
    while (cursor.hasNext()) {
        Status status = new Status("success", cursor.next()); 
        statusList.add(status);
    }

    return statusList;

} catch(Exception e) {
    e.printStackTrace();
}
rahul kale
  • 11
  • 5
  • The below code solved my issue. `JSONObject lastobj = new JSONObject(); lastobj.put("building_Id", building_Id); lastobj.put("floor_Id", floor_Id); JSONArray pointsArr = new JSONArray(); //Iterate the cursor while (cursor.hasNext()) { JSONObject pointsObj = new JSONObject(); DBObject dbObj = cursor.next(); pointsObj.put("x", dbObj.get("x")); pointsObj.put("y", dbObj.get("y")); pointsArr.add(pointsObj); } lastobj.put("points",pointsArr); return lastobj;` – BMAM Jan 31 '18 at 12:39