嗨, 我正在做REST網絡服務。 當我請求GET/POST方法時,響應是200 OK。 但我想獲得關鍵,價值的迴應。所以我寫這樣的:如何解決JSON響應?
@GET
@Produces("application/json")
public Response getContainer(@Context HttpHeaders headers){
mongoDAOImpl impl=new mongoDAOImpl();
Mongo mongo=impl.getConnection("127.0.0.1","27017");
DB db=impl.getDataBase(mongo,"public");
DBCollection coll=impl.getColl(db,"public");
mongoDTO dto=new mongoDTO();
try{
DBCursor dbcur=impl.findAllRecords(coll);
DBObject o;
while(dbcur.hasNext()){
o=dbcur.next();
}
if(dbcur==null){
return Response.status(Response.Status.NOT_FOUND).build();
}else{
String respStr=dto.toJson(false);
return Response.ok(respStr).header("Specification-version", "1.0").build();
}
}
catch(Exception ex){
ex.printStackTrace();
return Response.status(Response.Status.NOT_FOUND).tag("Container Read Error : " + ex.toString()).build();
}
}
但我得到的結果是
GET /web/jersey/resources/cloud/juniarto
HTTP/1.1
User-Agent: curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0
OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
Host: localhost:26235
Accept: */*
HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0
Server: GlassFish Server
Open Source Edition 3.0.1
Specification-version: 1.0
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 06 Apr 2011 00:56:48 GMT
Connection #0 to host localhost left intact
Closing connection #0 Error:org.codehaus.jackson.JsonGenerationException: Can not write a field name, expecting a values
什麼建議嗎?