title: How to return JsonArray in a Java Response object
body: <p>I'm trying to implement java based web-service server which returns to Json  and java script based web-service client. Here is my java part : </p>
  
  <pre><code>@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&lt;Recommendation&gt; 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();
              }
          }
  }
  </code></pre>
  
  <p>Now, when I call this function from my js client, I got :</p>
  
  <pre><code>POST http://localhost:8080/myapp/recs 500 (Request failed.)
  </code></pre>
  
  <p>But when I replace the for loop and return with the following code snipped I got response in my js correctly. </p>
  
  <p>Changed part : </p>
  
  <pre><code>// 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();
  </code></pre>
  
  <p>So, I wonder what might be problem ? Since its my first time with web-services, I have some difficulty to debug my code.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>By the way, I get also another error when I try to first version of getRecommendations() functions(with loop)</p>
  
  <pre><code>     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.
  </code></pre>
  
  <p>But as I said, When I remove the loop and put second code snipped into getRecommendations() functions, both of the errors are gone and I get the response in my website.</p>
  
  <p><strong>EDIT2</strong></p>
  
  <p>When I changed the loop and return statement of getRecommendations() function with the below I again <strong>get</strong> the same errors </p>
  
  <pre><code> 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();
  </code></pre>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>As far as I understood, <code>createObjectBuilder().build()</code> or <code>JsonArrayBuilder.build()</code> return an  <code>JSON object</code> and below of this build statement in my getRecommendations() function is not even run. So, I think my problem how could I give Access-Control-Allow-Origin permission to this object?</p>