简体   繁体   English

泽西岛的JSON响应不起作用

[英]JSON response with Jersey is not working

I'm developing a Web Service with Jersey, I'm having problems because when I return the JSON response to the browser I get this error message: 我正在使用Jersey开发Web服务,但是遇到了问题,因为当我将JSON响应返回到浏览器时,出现以下错误消息:

A message body writer for Java class org.json.JSONObject, and Java type class org.json.JSONObject, and MIME media type application/json was not found. 找不到Java类org.json.JSONObject和Java类型类org.json.JSONObject和MIME媒体类型application / json的消息正文编写器。

This is what I coded: 这是我编写的代码:

 @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();
  }

On the same java class I have a working example from another website: 在同一个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();
      }

The two peaces of code are very similar each to each other, but I don't understand why mi part isn't working. 两种代码的和平彼此非常相似,但是我不明白为什么mi part无法正常工作。 I just want to return the JSON, like in the example but without the String. 我只想返回JSON,就像示例中一样,但是没有String。

Thanks 谢谢

I solved! 我解决了! I was very stuck so I didn't see how much easy is the solution I just had to transform JSON to String: 我非常困惑,所以我看不出将JSON转换为String的解决方案有多么容易:

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

With this is everything solved. 这样,一切都解决了。 I'm very embarrassed. 我很不好意思

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

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