0

I am making an ajax POST request from the client. My Play Framework controller makes a request to a cross domain server which returns JSON. I then want to forward this JSON to the client. When I call Promise<JsonNode>.toString(), It appears I receive a memory address. How can I get the actual JSON back to the client?

    public static Result addVenue() {

      final Map<String, String[]> values = request().body().asFormUrlEncoded();
      String queryString = values.get("venueName")[0]  + ",+" + values.get("venueAddress")[0] + ",+" + values.get("venueCity")[0] + ",+" + values.get("venueState")[0] + "+" + values.get("venueZip")[0];
      String queryURL = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=" + queryString + "&key=" + "AIzaSyD1xSgKUnEZ_tM7qzcEAeM-SJBxPFhIpaU";
      queryURL = queryURL.replaceAll(" ", "+");

      Promise<JsonNode> jsonPromise = WS.url(queryURL).get().map(
              new Function<WSResponse, JsonNode>() {
                  public JsonNode apply(WSResponse response) {
                      JsonNode json = response.asJson();
                      return json;
                  }
              }
      );

      response().setHeader("Access-Control-Allow-Origin", "*");
      response().setHeader("Allow", "*");
      response().setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS");
      response().setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Referer, User-Agent");
      return ok(jsonPromise.toString());
}
4
  • Did you read the docs? Answer is right there. playframework.com/documentation/2.3.x/JavaAsync Commented Feb 6, 2015 at 5:57
  • @Ryan yes I did, however, the documentation refers to returning the body of a request as son, not getting a new response from an external web service. Commented Feb 6, 2015 at 18:35
  • Just curious, why was this -1? Commented Feb 8, 2015 at 0:10
  • I didn't downvote, but it's probably because the answer is in the documentation. playframework.com/documentation/2.3.x/JavaWS Commented Feb 8, 2015 at 4:48

2 Answers 2

2

Return a promise of result instead:

public static Promise<Result> addVenue() {
    return WS.url(URL).get().map((response) -> {
        return ok(response.asJson());
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I was able to figure it out last night finally. My code is a little different. I will post it below.
0

I was finally able to return the JSON using the following code. My implementation is a little different than another answer provided. I will provide it just in case the other one doesn't work for some people.

 final Promise<Result> resultPromise = WS.url(queryURL).get().map(
            new Function<WSResponse, Result>() {
                public Result apply(WSResponse response) {
                    Logger.info(response.asJson().toString());
                    return ok(response.asJson().toString());
                }
            }
 );
 return resultPromise;

1 Comment

This is the same as above, but for Java 7 :) Note that you don't need to use ".toString()" to send Json in the response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.