title: java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId In Mongo DB for @Path Rest API
body: <p>I am using @Path annotation to fetch data on the basis of 'State' parameter from mongo DB. While executing the code, I am getting the above exception.</p>
  <p>The code:</p>
  <pre><code>//State.Java
      @GET  
      @Path(&quot;/{State}&quot;)
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.APPLICATION_JSON)
      public Object[] addBuild2(@PathParam(&quot;State&quot;) String State  ) throws UnknownHostException
            
      { System.out.println(&quot;ïnside Location&quot;);
      Location loc = new Location();
      
      List&lt;DBObject&gt; basicDBList=(List&lt;DBObject&gt;) loc.getState2();
          System.out.println(&quot;ïnside State2===&gt;&quot;+basicDBList);
          return basicDBList.toArray();
          
      }
  
  //Location.Java
  public List&lt;DBObject&gt; getState2() throws UnknownHostException {
      DB db=ConnectToDB.getConnection();
      DBCollection collection = db.getCollection(&quot;location&quot;);
      BasicDBObject obj = new BasicDBObject();
       List&lt;BasicDBObject&gt; andQuery = new ArrayList&lt;BasicDBObject&gt;();
       andQuery.add(new BasicDBObject(&quot;Country&quot;, &quot;USA&quot;));
      
    andQuery.add(new BasicDBObject(&quot;State&quot;, new ObjectId(&quot;State&quot;)));
     obj.put(&quot;$and&quot;, andQuery); 
      
  
      BasicDBObject fields = new BasicDBObject();
          fields.put(&quot;_id&quot;, 0);
          
          DBCursor cursor = collection.find(obj, fields);
          List&lt;DBObject&gt; obj1 =cursor.toArray();
      
          
          
          System.out.println(&quot;&quot;+obj1);
              }
          
          return obj1;
  }
  }
  </code></pre>
  <p>The Stack trace:</p>
  <pre><code>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.&lt;init&gt;(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.
  </code></pre>
  <p>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.</p>