JSON Proxy in Java / Play! Framework

2019-06-24 18:47发布

I have a Play! application and from the JavaScript we now have run in to the Same Origin Policy Problem.

What I want is that JavaScript ajax calls go to our own server and that this server again route the json call to the external REST API.

My JavaScript use ajax to this url:

$.getJSON("http://mydomain.com/users", function(users) {
    //callback          
});

How can I easly make the server route to lets say:

public void getUsers(){
     // result = call www.otherdomain.org/api/users.json   What to do here?
     renderJson(result);
}

and the return the response?

Or can it be done dynamically somewhere by directly rerouting?

2条回答
\"骚年 ilove
2楼-- · 2019-06-24 19:16

here comes an example for doing async http calls (e.g. to facebook api)

WSRequest req = WS.url("https://graph.facebook.com/100001789213579");
Promise<HttpResponse> respAsync = req.getAsync();
HttpResponse resp = await(respAsync);

JsonElement jsonResp = resp.getJson();
JsonObject jsonObj = new JsonObject();
jsonObj.add("facebook-response", jsonResp);

renderJSON(jsonObj);
查看更多
\"骚年 ilove
3楼-- · 2019-06-24 19:31

You can use the WS class to call another URL as a web service and retrieve the answer.

See an example here

查看更多
登录 后发表回答