java - RestFul java with mongodb 如何添加@QueryParam

标签 java json mongodb rest

我在 java 和 mongodb 上的 Restful web 服务有问题。

这是我的类(class)

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;
}

这是我的 Web 服务类

@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","city":"CHICOPEE","loc":[-72.607962,42.162046],"pop":23396,"state":"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 新手。我试过用谷歌搜索它,但找不到我的解决方案。 这样的话我用MYSQL数据库开发就没问题了。 但是对于 MongoDB 我真的不知道。 谁能帮帮我?抱歉我的英语不好。

最佳答案

@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,你的 url 应该是,

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

关于java - RestFul java with mongodb 如何添加@QueryParam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24662034/

相关文章:

node.js - Mongoose 模式创建

java - Java 中的 GUI 电子邮件 validator

java - 使用 Java 捕获和显示图像

json - ARKit 4.0 – 是否可以将 ARWorldMap 数据转换为 JSON 文件?

node.js - SQLite:选择和删除

arrays - 从 Mongo 中的嵌入式数组中按值提取项目

java - 内部枚举或类是否有效?

java - osmdroid - 更高的缩放级别?

php - 尝试在 Xcode NSCocoaErrorDomain Code=3840 中解析 JSON

jquery - 数据表错误: Requested unknown parameter '1' from the data source for row 0