![](/img/trans.png)
[英]xml response not working but json is working Rest Api using Jersey
[英]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.