繁体   English   中英

泽西岛的JSON响应不起作用

[英]JSON response with Jersey is not working

我正在使用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 part无法正常工作。 我只想返回JSON,就像示例中一样,但是没有String。

谢谢

我解决了! 我非常困惑,所以我看不出将JSON转换为String的解决方案有多么容易:

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

这样,一切都解决了。 我很不好意思

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM