【问题标题】:JSON response with Jersey is not working与泽西岛的 JSON 响应不起作用
【发布时间】: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?

标签: java json maven jersey


【解决方案1】:

我解决了!我非常卡住,所以我没有看到我只需将 JSON 转换为 String 的解决方案有多简单:

return Response.status(200).entity(json.toString()).build();

这一切都解决了。我很尴尬。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 2015-01-05
    • 1970-01-01
    • 2017-09-18
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多