gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:43:43 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com