正在开发 REST API。这里的示例工作正常,但我对这里的某些内容感到非常困惑。我的方法 getAll 返回一个字符串,但该方法已被注释为 Produce MIME MediaType of JSON。我的问题是,最终返回的响应(doc)是 JSONObject 还是只是 json 字符串(因此是 JSON 格式的字符串)。这样我就可以知道在收到响应时如何处理。
@Path("/membership/{pageNumber}")
@GET
@Produces("application/json")
public String getAll(@PathParam("pageNumber") String pageNumber){
Map<String , String > params = new HashMap<>();
String doc = HTTPClient.sendGet("localhost:9500/users/list/membership/field/"
+pageNumber, params, 60);
return doc;
}
这对我的文档也很有帮助。 感谢所有的努力,谢谢。
最佳答案
My question is , is the final returned response (doc) a JSONObject or just a json String(Thus a String in JSON format)
是的,它会返回json字符串,如果你想一起发送JSON和HTTP状态,你可以使用Response作为您的返回类型。
示例:
@GET
@Path("/testGet")
@Produces("application/json")
public Response testGet() {
String json = //Create JSON String
return Response.ok(json, MediaType.APPLICATION_JSON).build();
}
关于javax.ws.rs : What is Actually Returned by a Method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38609944/