嗨,我正在做REST web服务。当我请求GET/POST方法时,响应是200 OK。但我想得到的答案是key,value。所以我这样写:
@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
有什么建议吗?
您使用的是哪种版本的Jersey?您的JSON输出是否包含任何数组?
看起来你正在经历JERSEY-478。尝试更新到Jersey 1.5或更高版本。