[英]How to access JSONArray inside of a JSONArray in a JSON object response
[英]How to return JsonArray in a Java Response object
我正在嘗試實現基於Java的Web服務服務器,該服務器返回到基於Json和java腳本的Web服務客戶端。 這是我的java部分:
@Path("/myapp")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class MyRecommender {
@POST
@Path("/recs")
public Response getRecommendations() {
//Assume recommendation List contains some Recommendation objects
//I removed it for simplicity.
List<Recommendation> recommendation;
JsonArrayBuilder builder = Json.createArrayBuilder();
for (Recommendation rec : recommendations) {
builder.add(Json.createObjectBuilder().add("firstPersonName", "\"" + rec.getFirstPerson().getName() + "\"")
.add("firsPersonURL", "\"" + rec.getFirstPerson().getURL() + "\"")
.add("secondPersonName", "\"" + rec.getSecondPerson().getName() + "\"")
.add("secondPersonURL", "\"" + rec.getSecondPerson().getURL() + "\"")
.add("score", "\"" + rec.getSimilarity() + "\""));
}
JsonArray jsonData = builder.build();
return Response.ok(jsonData, MediaType.APPLICATION_JSON).header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST").allow("OPTIONS").build();
}
}
}
現在,當我從我的js客戶端調用此函數時,我得到:
POST http://localhost:8080/myapp/recs 500 (Request failed.)
但是當我替換for循環並返回以下代碼時,我在js中得到了正確的響應。
改變部分:
// remove for loop and change jsonData type.
String jsonData = "{\"name\":\"John\"}";
return Response.ok(jsonData, MediaType.APPLICATION_JSON).header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST").allow("OPTIONS").build();
所以,我想知道可能會出現什么問題? 自從我第一次使用Web服務以來,我在調試代碼時遇到了一些困難。
編輯
順便說一句,當我嘗試第一個版本的getRecommendations()函數(帶循環)時,我還得到另一個錯誤
XMLHttpRequest cannot load http://localhost:8080/myapp/recs.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.
The response had HTTP status code 500.
但正如我所說,當我刪除循環並將第二個代碼剪切到getRecommendations()函數時,兩個錯誤都消失了,我在我的網站上得到了響應。
EDIT2
當我改變循環並返回getRecommendations()函數的語句時,我再次得到相同的錯誤
JsonObject value = Json.createObjectBuilder()
.add("name", "John").build();
return Response.ok(value, MediaType.APPLICATION_JSON).header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST").allow("OPTIONS").build();
編輯3
據我所知, createObjectBuilder().build()
或JsonArrayBuilder.build()
返回一個JSON object
,我的getRecommendations()函數中的這個構建語句的下面甚至沒有運行。 所以,我認為我的問題是如何為這個對象提供Access-Control-Allow-Origin權限?
如果可以使用第三方庫,在這種情況下使用Google的Gson可能是一種有效的解決方案。 將模型轉換為JSON時非常方便。 你能做的就是這樣。
ArrayList<Recommendation> recommendationList = new ArrayList<Recommendation>();
Gson gson = new Gson(); String jsonData = gson.toJson(reccomendationList);
要將它用作POM文件中的依賴項,您可以執行此操作。
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
<scope>compile</scope>
</dependency>
你應該嘗試最簡單的方法:
return Response.ok().entity(recommendation).header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST").allow("OPTIONS").build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.