JSON proxies in Java / Play! Framework

I have a game! applications and from JavaScript, which we now run in the same problem with the original policy.

I want the ajax JavaScript requests to go to our own server and that server redirect the json call to the external REST API again.

My JavaScript uses ajax for this URL:

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

How can I easily make a server route to say:

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

and return the answer?

Or can this be done dynamically somewhere by direct redirection?

+4
source share
2 answers

here is an example for making asynchronous http calls (e.g. for 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); 
+3
source

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

See an example here.

+2
source

Source: https://habr.com/ru/post/1381903/


All Articles