2

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

Community
  • 1
  • 1
IcedDante
  • 6,145
  • 12
  • 57
  • 100
  • Take a look into the docs, can't do it now, but I'll show you some simple samples later (or maybe someone else will...) anyway why do you think there;s nor `render` method anymore ? :) – biesior Jan 02 '13 at 19:45
  • I just pullled it into eclipse, the render method returned an error... so I just sort of assumed it wasn't working. I guess you are saying that it is there- let me double check. – IcedDante Jan 02 '13 at 20:16
  • 1
    ooooh... that's Eclipse's fault :) check samples and docs, also search Stack Overflow for topics of Play and working in Eclipse. – biesior Jan 02 '13 at 20:30

1 Answers1

4

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).

biesior
  • 55,576
  • 10
  • 125
  • 182
  • Biesior- awesome reply. Thank you so much. One question tho: here the user supplies the content type via the "format" parameter. If you look at the first link I posted in my question, it seems like the response-type is figured out from the request. Is that solution still possible here? – IcedDante Jan 02 '13 at 23:12
  • Are you gonna to use this API with jQuery only or with other - 3rd party clients too ? – biesior Jan 02 '13 at 23:40
  • I wasn't planning on using jQuery at all- right now using a couple of Jersey clients to connect to it for different purposes... but why would these services be specific to any client? – IcedDante Jan 03 '13 at 00:06
  • Another question: can you give me some insight into why you named the XML version of your view "xml.scala.html"? Shouldn't it be something like user.scala.xml and be located under the views.user directory? – IcedDante Jan 03 '13 at 01:44
  • I asked about jQuery, cause I'm lazy and I don't want to prepare samples in many techniques ;) Check my edited post, I moved whole code to github, so you can clone and test it. – biesior Jan 03 '13 at 12:29