2014-07-09 25 views
2

我有我的Restful web服务在java和mongodb上的问题。RestFul java与mongodb如何添加@QueryParam

这里是我的类

public JSONArray returnAll() throws Exception{ 
    MongoClient mongoClient = mongoConnection(); 
    DBCollection collection = mongoClient.getDB("mydb").getCollection("zip"); 
    BasicDBObject query = new BasicDBObject(); 
    query.put("city","CHICOPEE"); 
    DBCursor cursor = collection.find(query); 
    JSON json = new JSON(); 
    String serialize = json.serialize(cursor); 
    JSONArray AllJson = new JSONArray(serialize); 
    return AllJson; 
} 

,这是我的WebService类

@Path("/All") 
@GET 
@Produces(MediaType.APPLICATION_JSON) 
public Response returnDatabaseAll() throws Exception{ 
    String returnString = null; 
    JSONArray json = new JSONArray(); 
    try{ 
     greatontimeSchema dao = new greatontimeSchema(); 
     json = dao.returnAll(); 
     returnString = json.toString(); 
    }catch (SQLException ex){ 
     ex.printStackTrace(); 
     return Response.status(500).entity("Server was not able").build(); 

    }catch(Exception e){ 
     e.printStackTrace(); 
     return Response.status(500).entity("Server was not able").build(); 
    } 
    return Response.ok(json).build(); 
} 

这是为我工作。它是这样返回正确的JSON数据。当我打电话

http://192.168.1.5:8080/com.projecttest.JSMongo/api/mongoWS/All 

[{ “_id”: “01013”, “城市”: “CHICOPEE”, “禄”: - 72.607962,42.162046], “啪”:23396, “状态”: “MA”},{“_ id”:“01020”,“city”:“CHICOPEE”,“loc”:[ - 72.576142,42.176443],“pop”:31495,“state”:“MA”}]

但是,如果我想@QueryParam添加到我的班这样

public JSONArray returnAll (String city) throws Exception{ 
    MongoClient mongoClient = mongoConnection(); 
    DBCollection collection = mongoClient.getDB("mydb").getCollection("zip"); 
    BasicDBObject query = new BasicDBObject(); 
    query.put("city",city); 
    DBCursor cursor = collection.find(query); 
    JSON json = new JSON(); 
    String serialize = json.serialize(cursor); 
    JSONArray AllJson = new JSONArray(serialize); 
    return AllJson; 
} 

并改变我的web服务类这

@Path("{city}") 
@GET 
@Produces(MediaType.APPLICATION_JSON) 
public Response returnDatabaseAll(@QueryParam("city") String city) throws Exception{ 
    String returnString = null; 
    JSONArray json = new JSONArray(); 
    try{ 
     greatontimeSchema dao = new greatontimeSchema(); 
     json = dao.returnAll(city); 
     returnString = json.toString(); 
    }catch (SQLException ex){ 
     ex.printStackTrace(); 
     return Response.status(500).entity("Server was not able").build(); 

    }catch(Exception e){ 
     e.printStackTrace(); 
     return Response.status(500).entity("Server was not able").build(); 
    } 
    return Response.ok(json).build(); 
} 

当我输入网址这样

http://192.168.1.5:8080/com.projecttest.JSMongo/api/mongoWS/CHICOPEE 

它返回

[]

我在java的新手。我试图Google,但我找不到我的解决方案。 在这种情况下,我用MYSQL数据库开发它没有问题。 但与MongoDB我真的不知道。 任何人都可以帮我吗?对不起,我英文很差。

+3

使用'@ PathParam',你有'@ QueryParam'。 –

+0

太棒了!是工作 !!非常感谢。 @DaveMorrissey – greatontime

回答

1
@GET 
@Produces(MediaType.APPLICATION_JSON) 
public Response returnDatabaseAll(@QueryParam("city") String city) throws Exception{ 
String returnString = null; 
JSONArray json = new JSONArray(); 
try{ 
    greatontimeSchema dao = new greatontimeSchema(); 
    json = dao.returnAll(city); 
    returnString = json.toString(); 
}catch (SQLException ex){ 
    ex.printStackTrace(); 
    return Response.status(500).entity("Server was not able").build(); 
}catch(Exception e){ 
    e.printStackTrace(); 
    return Response.status(500).entity("Server was not able").build(); 
} 
return Response.ok(json).build(); 
} 

您仍然可以使用Queryparam上述和您的网址应该是,

http://192.168.1.5:8080/com.projecttest.JSMongo/api/mongoWS?city=CHICOPEE