Reputation: 71
I am using REST API to fetch data using @Form param from Mongo DB and got the exception: 'Invalid hexadecimal representation of ObjectId'. The syntax seems to be correct, not sure whats going wrong there. I am passing new ObjectId (id) in the rest parameter. The code is as below:
//Country.java
package com.speed.infoaxon;
import java.io.IOException;
import java.net.UnknownHostException;
import org.bson.types.ObjectId;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
public class Country {
public BasicDBObject addDemo(long _id ) throws IOException {
DB db=ConnectToDB.getConnection();
DBCollection collection = db.getCollection("demo");
BasicDBObject buildList = null;
BasicDBObject document = new BasicDBObject();
document.put("_id",new ObjectId("id"));
collection.save(document);
return buildList;
}
}
//getResponse.java
package com.speed.infoaxon;
import java.io.IOException;
import java.net.UnknownHostException;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
@Path("/add")
public class GetResponse {
@POST
@Path("/addDemo")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED} )
public BasicDBObject addDemo(@FormParam("_id") long _id) throws IOException
{
System.out.println("inside demo");
Country d = new Country();
BasicDBObject basicDBList=d.addDemo(_id);
return basicDBList;
}
}
Please let me know where is the issue. Thanks in advance.
Upvotes: 4
Views: 19337
Reputation: 266
document.put("_id",new ObjectId("id"));
you're using "id" in quotes which means its id in string you need to pass in the actual id
Upvotes: 2