简体   繁体   中英

RESTful on Play! 2.0 Framework

This post from a while back: RESTful on Play! framework Illustrated a very elegant approach to designing RESTful Web Services on Play! 1.x with Java.

I am working with Play! v2 and am wondering how easy it is to migrate this approach to the new version. Right off the bat I see that the render method doesn't exist anymore, so I was wondering if any major architecture changes in the framework prevent this process from being used.

This post: Play 2.0 RESTful request post-processing illustrates some of the 2.0 based processing, but it is done in Scala and I am having a devil of a time parsing through it

Whole process in Play 2.0 will be VERY similar the only small differences will be in the syntax, take a look into the documentation .

routes

# REST actions
GET     /user/:id    controllers.Rest.user(id: Int)
POST    /user        controllers.Rest.createUser
PUT     /user/:id    controllers.Rest.updateUser(id: Int)
DELETE  /user/:id    controllers.Rest.deleteUser(id: Int)

Edit:

Of course you can use Accept header instead of format parameter, however param is just... easier to set. You can also use ie. user.scala.xml name for the view, and in the controller render it with: return ok(views.xml.user.render(user).body());

I published that sample on the GitHub with some fixes, so you can try it yourself: play-simple-rest

Note that you need to keep Rest.java controller (and its routes) while everything in Application is just for displaying and demonstration (ie making GET/POST/PUT/DELETE calls in controllers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM