JSON Proxy in Java / Play! Framework

自作多情 提交于 2019-12-23 12:26:40

问题


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?


回答1:


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



回答2:


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

See an example here



来源:https://stackoverflow.com/questions/8183199/json-proxy-in-java-play-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!