Playframework 项目的 RESTful API
我是 playframework 的新手。我正在计划一个大项目,该项目将为其他应用程序公开许多端点,以便它们可以与其集成;我们是否有任何可以在 playframework 环境中使用的扩展或库(例如 django-piston)?
I am new to playframework. I'm planning a big project which will be exposing number of endpoints for other applications so that they can integrate with it; Do we have any extensions or libraries like django-piston that can be use within playframework environment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 playframework 中,要创建 API,请执行以下操作:
conf/routes
编写您的路由。您可以在此处定义常用的 HTTP 路由(GET、PUT、POST 等)。每个路由都会有一个到控制器的链接,例如,以下路由由 Hello 控制器提供服务:GET /hello Application.controllers.Hello.saySomething
app/controllers
文件夹中创建Hello.scala
文件。现在在控制器中编写方法,例如:def saySomething = Action {Ok("Im SayHello")}
如果然后运行您的应用程序 (
sbtcompile run
)您应该在localhost:9000
处看到I’m say hello
。此外,您还可以使用 Json 或 XML 来代替字符串。如果您想向其他应用程序“提供”一些具有标准和结构的数据,这在现实世界的示例中至关重要。不想在这里讨论太多细节,但是play对Json有很好的支持,你可以还有什么?
我想在这里提到的另外两个主题:
模型:您可以在
app/models
文件夹中定义模型。这些模型负责与数据库或其他外部源对话;比如S3。视图:如果您想通过 api 创建完整的堆栈应用程序,请在
app/views
文件夹中定义视图文件。如果您想使用任何脚本语言和样式表,那么您需要在/public
文件夹中定义它们。In playframewrork, to make an API do the following:
conf/routes
to write your routes. You can define the usual HTTP routes here (GET, PUT, POST, etc.). Each route will have a link to a controller, for example, the following route is served by Hello controller:GET /hello Application.controllers.Hello.saySomething
Hello.scala
file withinapp/controllers
folder. Now write the method within the controller, such as:def saySomething = Action {Ok("Im saying hello")}
If then you run your app (
sbt compile run
) you should seeI'm saying hello
atlocalhost:9000
. Further more you can use Json or XML instead of string. This is essential in real world examples if you want to "feed" other apps some data which has a standard and structure. Don't want to go into much detials here, but play has a great support for Json, you can read more here.What Else?
Two other topics which I would like to mention here:
Models: You can define your models at
app/models
folder. These models are responsible to talk to DB or another external sources; such as S3.Views: In case you want to make a full stack application from your api, define your views file at the
app/views
folder. If you want to use any script language and stylesheet then you need to define them in the/public
folder.无需在 Play 上安装扩展!,最简单的是使用 REST API 即可一切就绪!
看看freedompeace给的教程,有问题再回来。
No need to install extensions on Play!, the simplest thing is to use a REST API and all is ready!
Look at the tutorial given by freedompeace, and come back when you have questions.
Play 框架构建在 Netty 之上,可以轻松编写 HTTP 服务,无论这些服务是否使用 HTML、JSON、XML 进行响应。创建服务所需要做的就是在
conf/routes
中为应用程序资源设置路由,如下所示。然后使用如下内容实现控制器方法:
阅读此内容以获取有关路由和控制器。
然后使用 Anorm 或其他库来创建模型来实现模型。
Play framework is build on top of Netty to make it easy to write HTTP services, it does not matter if these respond with HTML, JSON, XML. What you have to do to create services is to setup routes for your application resources in
conf/routes
like this.Then implement the controller methods with something like this:
Read this for more details on routing and controllers.
Then implement your models using Anorm or some other library to create your models.
实际上 Play Framework 是一个全栈 Web 框架。使用 Netty/Akka-http 作为服务器,它不需要容器来运行。
使用 sbt 构建项目,您可以添加第三方库作为用 Scala 和 Java 编写的依赖项。
如果只是RESTFul API,只需关注conf/routes并定义你的url,然后完成你的业务逻辑。 Play 框架的文档可以在此处找到。
Actually Play Framework is a full stack web framework. With Netty/Akka-http as it's server, it don't need a container to run.
With sbt to build your project, you can add third part libs as dependencies both write in Scala and Java.
If just RESTFul API, just focus on conf/routes and to define your url, then finish your business logics. Docs for Play Framework can be found here.