java.lang.IllegalArgumentException:ObjectId的无效十六进制表示形式在Mongo数据库中为@Path Rest API

时间:2017-09-04 04:28:03

标签: mongodb rest path database

我正在使用@Path注释来获取基于' State'来自mongo DB的参数。在执行代码时,我得到了上述异常。     代码如下:

//State.Java
    @GET  
    @Path("/{State}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Object[] addBuild2(@PathParam("State") String State  ) throws UnknownHostException

    { System.out.println("ïnside Location");
    Location loc = new Location();

    List<DBObject> basicDBList=(List<DBObject>) loc.getState2();
        System.out.println("ïnside State2===>"+basicDBList);
        return basicDBList.toArray();

    }

//Location.Java
public List<DBObject> getState2() throws UnknownHostException {
    DB db=ConnectToDB.getConnection();
    DBCollection collection = db.getCollection("location");
    BasicDBObject obj = new BasicDBObject();
     List<BasicDBObject> andQuery = new ArrayList<BasicDBObject>();
     andQuery.add(new BasicDBObject("Country", "USA"));

  andQuery.add(new BasicDBObject("State", new ObjectId("State")));
   obj.put("$and", andQuery); 


    BasicDBObject fields = new BasicDBObject();
        fields.put("_id", 0);

        DBCursor cursor = collection.find(obj, fields);
        List<DBObject> obj1 =cursor.toArray();



        System.out.println(""+obj1);
            }

        return obj1;
}
}

The error Stack trace is as below:

HTTP Status 500 – Internal Server Error

Type Exception Report

Message invalid hexadecimal representation of an ObjectId: [State]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [State]
    org.bson.types.ObjectId.parseHexString(ObjectId.java:519)
    org.bson.types.ObjectId.<init>(ObjectId.java:233)
    com.speed.infoaxon.Location.getState2(Location.java:116)
    com.speed.infoaxon.GetProjectLocationResponse.addBuild2(GetProjectLocationResponse.java:54)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)

com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)         com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)

Note The full stack trace of the root cause is available in the server logs.


Please tell what the issue is while using @Path for Rest API as I am using 'State' as a parameter to fetch all the data for the corresponding state. Thanks in advance.

0 个答案:

没有答案