简体   繁体   English

Java / Play中的JSON代理!骨架

[英]JSON Proxy in Java / Play! Framework

I have a Play! 我玩了! application and from the JavaScript we now have run in to the Same Origin Policy Problem. 应用程序和我们现在运行的JavaScript到同源策略问题。

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. 我想要的是JavaScript ajax调用转到我们自己的服务器,并且该服务器再次将json调用路由到外部REST API。

My JavaScript use ajax to this url: 我的JavaScript使用ajax到这个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? 或者可以通过直接重新路由在某处动态完成?

here comes an example for doing async http calls (eg to facebook api) 这里有一个用于执行异步http调用的示例(例如,对于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);

You can use the WS class to call another URL as a web service and retrieve the answer. 您可以使用WS类将另一个URL作为Web服务调用并检索答案。

See an example here 在这里查看示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM