[英]How to turn a url segment into a single parameter in Play! Framework
问题
我希望url段here/is/a/request/to/letsay/a/user
在Play中变成一个单一参数orginalRequest
! 路线
为什么
为了使用JSON和AJAX解决同源策略,我们决定在我们自己的服务器上运行一个Web服务客户端 ,将所有JSON调用重定向到外部REST API。
Play中有没有办法! 框架路由文件,我可以威胁/ api /之后的单个参数吗?
在途中:
GET /api/fromhere/is/what/iwant/as/single/parameter App.getFromOtherDomain
在App中:
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());
}
在Play 1.x中,您使用RegEx:
路线:
GET /files/{<[a-z0-9/\.]*>name} Application.download(name)
在Play 2.0中你可以做得更短:
路线:
GET /files/*name Application.download(name)
呼叫:
http://yourserver/files/public/downloads/hello.png
在下载操作中, name
将等于"public/downloads/hello.png"
。
从您的控制器,您可以使用request.path和request.uri提取所需的信息,然后使用提取的信息调用您的其他方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.