How to turn a URL segment into one parameter in Play! Framework

Problem

I want the URL segment here/is/a/request/to/letsay/a/user turn into one orginalRequest parameter in Play! route

Why

To solve the same origin policy using JSON and AJAX, we decided to create a web services client on our own server that redirects all JSON calls to the external REST API.

Is there a way in the game! A raster route file that I can threaten to ever appear after / api / as one single parameter?

In the route:

 GET /api/fromhere/is/what/iwant/as/single/parameter App.getFromOtherDomain 

In the application:

 public void getFromOtherDomain(String orginalRequest){ WSRequest req = WS.url("https://api.otherdomain.com/" + orginalRequest); Promise<HttpResponse> respAsync = req.getAsync(); HttpResponse resp = await(respAsync); renderJSON(resp.getJson()); } 
+4
source share
2 answers

In Play 1.x you are using RegEx:

Routes

  GET /files/{<[a-z0-9/\.]*>name} Application.download(name) 

In Play 2.0, you can make this a little shorter:

Routes

 GET /files/*name Application.download(name) 

Call:

 http://yourserver/files/public/downloads/hello.png 

In the upload action, name will be "public/downloads/hello.png" .

+5
source

From your controller, you can use request.path and request.uri to extract the necessary information, and then call another method using the information extracted.

+1
source

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


All Articles