[英]Is play! Framework 2.0 suitable for creating a REST API?
I have developed a REST API using Play! 我使用Play开发了一个REST API! Framework 1.2.4, and I have a strong liking for the framework.
框架1.2.4,我对框架非常不满意。 The simplicity and the rapid development cycle helped me achieve this in a fraction of the time I would have taken had I gone the traditional Java EE route.
简单性和快速的开发周期帮助我实现了这一目标,只需花费我传统Java EE路线的一小部分时间。
Now that I am exploring using Play! 现在我正在使用Play进行探索! 2.0.3 for my next project.
2.0.3用于我的下一个项目。 I see that while the framework has been enhanced and makes it even easier to develop web-apps , the same cannot be said about REST API's .
我看到虽然框架已得到增强,并且使开发Web应用程序变得更加容易,但关于REST API的情况也是如此 。 My app will not have any HTML whatsoever - I will just respond with XML or JSON or whatever data exchange format I decide to use in future.
我的应用程序将不会有任何HTML - 我将仅使用XML或JSON或我将来决定使用的任何数据交换格式进行响应。
So, the question is: 所以,问题是:
Has anyone here used Play 2.0.x for exposing non-html pure REST API's?
有没有人在这里使用Play 2.0.x来暴露非HTML纯REST API?
Here are some of the factors I feel make it more difficult to develop pure REST API's in Play 2.0.x compared to 1.2.x. 以下是我认为与1.2.x相比,在Play 2.0.x中开发纯REST API更困难的一些因素。 Please correct my understanding if I am wrong.
如果我错了,请纠正我的理解。
In play! 在玩! 1.2.4, I content negotiation was build in to the framework.
1.2.4,我内容协商是建立在框架之上的。 There were options to define right in the routes file what content-type a request expects.
有一些选项可以在路径文件中定义请求所期望的内容类型。
GET /friends User.listFriends(format:'xml')
Then, in the controller, 然后,在控制器中,
public static void getFriends(){
render();
}
This would result in the views/xml/User/listFriends.xml
template being rendered automatically. 这将导致自动呈现
views/xml/User/listFriends.xml
模板。 To add support for JSON tomorrow, all I needed to do was to add a views/json/User/listFriends.json
template. 要明天添加对JSON的支持,我需要做的就是添加
views/json/User/listFriends.json
模板。
I do not see how this can be done in play! 我不知道如何在游戏中做到这一点! 2.0.x
2.0.x版本
After some trial and error, I figured out that one can create, for example, a listFriends.scala.xml in the views folder in play! 经过一些反复试验,我发现可以在play文件夹中创建一个listFriends.scala.xml! 2.0.
2.0。 Then, it needs to be invoked in the controller code as follows:
然后,需要在控制器代码中调用它,如下所示:
return ok(views.xml.listFriends.render())
; return ok(views.xml.listFriends.render())
;
However, Eclipse doesn't like this, because Eclipse does not know about the views.xml.listFriends
since it is generated only after play compilation completes. 但是,Eclipse不喜欢这样,因为Eclipse不知道
views.xml.listFriends
因为它仅在播放编译完成后生成。 Is there anything I'm missing here? 这里有什么我想念的吗?
In Play (Scala) you can do something like this: 在Play(Scala)中,您可以执行以下操作:
val myXMl = obtainXML();
return Ok(myXML).as("text/xml")
I'm not sure of the syntax in Java, but it would be equivalent: instead of creating a template, you generate the XML and then you send it to the user, setting the return type to "text/xml" (or json or whatever you need it to be). 我不确定Java中的语法,但它是等价的:不是创建模板,而是生成XML然后将其发送给用户,将返回类型设置为“text / xml”(或json或无论你需要它是什么)。
As Pere Villega explained, but with the Java syntax: 正如Pere Villega解释的那样,但是使用Java语法:
String xml = getXMLAsString();
return ok(xml).as("text/xml");
The as() method is part of the Status class . as()方法是Status类的一部分。
Or, an alternative is: 或者,另一种选择是:
String xml = getXMLAsString();
response().setContentType("text/xml")
return ok(xml);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.