O'Reilly logo

Play Framework Essentials by Julien Richard-Foy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Putting things together

You now have all the required knowledge to define HTTP endpoints for the list, create, and get entry points of the Shop service so that your HTTP endpoints can read both HTML form data and JSON data, and can return either HTML or JSON content according to the client's preferences.

For instance, here is the code for how the create endpoint can look like:

 val create = Action { implicit request => createItemFormModel.bindFromRequest().fold( formWithErrors => render { case Accepts.Html() => BadRequest(views.html.createForm(formWithErrors)) case Accepts.Json() => BadRequest(formWithErrors.errorsAsJson) }, createItem => { shop.create(createItem.name, createItem.price) match { case Some(item) => render { case Accepts.Html() => ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required