2014-07-09 3 views
2

Java 및 mongodb의 내 웹 서비스에 문제가 있습니다.RestFul java with 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", "도시": "치코 피", "LOC": [- 72.607962,42.162046, "팝업"23396, "상태" "MA"}, { "_ id": "01020", "city": "CHICOPEE", "loc": [- 72.576142,42.176443], "pop": 31495, "state": "MA"}]

하지만이

내 웹 서비스 클래스를이

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

처럼 내 클래스에 @QueryParam를 추가하고 변경하려는 경우 나는이

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

같은 URL을 입력 할 때 9,179,403,210

그것은

은 []

내가 자바 안돼서 반환합니다. Google에 시도했지만 해결책을 찾을 수 없습니다. 이 경우에는 MYSQL 데이터베이스로 개발합니다. 문제 없습니다. 그러나 MongoDB와 나는 정말로 몰라요. 누구도 도와 주실 수 있습니까? 나의 가난한 영어를 유감스럽게 생각한다. 당신이 QueryParam` @`이

+3

사용'@ PathParam'. –

+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(); 
} 

당신은 여전히해야 위 및 URL과 Queryparam를 사용할 수

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