gpt4 book ai didi

java - 从 Play Framework Controller 返回 JSON 字符串

转载 作者:行者123 更新时间:2023-12-02 05:00:06 25 4
gpt4 key购买 nike

我正在从客户端发出 ajax POST 请求。我的 Play Framework Controller 向跨域服务器发出请求,该服务器返回 JSON。然后我想将此 JSON 转发给客户端。当我调用Promise<JsonNode>.toString()时,看来我收到了一个内存地址。如何将实际的 JSON 返回给客户端?

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

最佳答案

返回结果 promise :

public static Promise<Result> addVenue() {
return WS.url(URL).get().map((response) -> {
return ok(response.asJson());
});
}

关于java - 从 Play Framework Controller 返回 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28355760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com