【发布时间】:2016-11-15 01:22:04
【问题描述】:
我正在使用 Jersey 开发 Web 服务,我遇到了问题,因为当我将 JSON 响应返回给浏览器时,我收到了以下错误消息:
找不到 Java 类 org.json.JSONObject、Java 类型类 org.json.JSONObject 和 MIME 媒体类型 application/json 的消息正文编写器。
这是我编码的:
@Path("details")
@GET
@Produces("application/json")
public Response questionDetails (){
JSONObject json = new JSONObject();
json.put("kind", "Yes/No");
json.put("tipeCode", 1);
return Response.status(200).entity(json).build();
}
在同一个 java 类上,我有一个来自另一个网站的工作示例:
@Path("test")
@GET
@Produces("application/json")
public Response convertFtoCfromInput() throws JSONException, SQLException {
JSONObject jsonObject = new JSONObject();
float celsius;
celsius = (f - 32)*5/9;
jsonObject.put("F Value", f);
jsonObject.put("C Value", celsius);
jsonObject.put("number", number);
jsonObject.put("statement", statement);
String result = "@Produces(\"application/json\") Output: \n\nJSON from Path: \n\n" + jsonObject;
return Response.status(200).entity(result).build();
}
这两种代码彼此非常相似,但我不明白为什么 mi 部分不起作用。我只想返回 JSON,就像在示例中一样,但没有字符串。
谢谢
【问题讨论】:
-
使用正在运行的代码进行测试我注意到,如果我只返回没有字符串的 JSON 对象,我会遇到同样的错误,那么,我该如何返回 JSON?