RESTful API for playframework project
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 l开发者_运维知识库ike django-piston that can be use within playframework environment?
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 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.
GET /api/projects controllers.ProjectController.index
POST /api/projects controllers.ProjectController.create
GET /api/projects/:id controllers.ProjectController.show(id: Long)
PUT /api/projects/:id controllers.ProjectController.update(id: Long)
DELETE /api/projects/:id controllers.ProjectController.delete(id: Long)
Then implement the controller methods with something like this:
def index() = Action { implicit request =>
Ok(Json.toJson(Project.findAll(request.queryString))
}
Read this for more details on routing and controllers.
Then implement your models using Anorm or some other library to create your models.
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.
In playframewrork, to make an API do the following:
- Define a route: Go to
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
- Define a controller: Then, make
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 see I'm saying hello
at localhost: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.
精彩评论