使用mongodb的RestFul java如何添加@QueryParam

时间:2014-07-09 19:16:54

标签: java json mongodb rest

我在java和mongodb上使用Restful Web服务时遇到问题。

这是我的班级

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"" LOC&# 34;:[ - 72.607962,42.162046]"弹出":23396"状态":" MA"},{" _id&#34 ;: #&34; 01020""城市":" CHICOPEE"" LOC":[ - 72.576142,42.176443]"弹出&#34 ;:31495"状态":" 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;
}

将我的网络服务类改为此

@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的新手。我试过谷歌,但我无法找到我的解决方案。 在这种情况下,我用MYSQL数据库开发它没问题。 但是对于MongoDB,我真的不知道。 有人可以帮帮我吗?抱歉我的英语不好。

1 个答案:

答案 0 :(得分: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