├── LICENSE ├── README.md ├── activator.properties ├── app.json ├── app ├── controllers │ └── Application.scala └── models │ └── Book.scala ├── build.sbt ├── conf ├── application.conf └── routes ├── project ├── build.properties └── plugins.sbt └── tutorial └── index.html /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Julien Richard-Foy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Simple REST API in Play 2 | ----------------------- 3 | 4 | An activator [template](https://typesafe.com/activator/templates) for implementing Json based [REST API](https://www.playframework.com/documentation/2.3.x/ScalaJsonHttp). 5 | 6 | [![Deploy to Heroku](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy) 7 | -------------------------------------------------------------------------------- /activator.properties: -------------------------------------------------------------------------------- 1 | name=simple-rest-scala 2 | title=Simple REST API working in Play 3 | description=A boilerplate to get simple REST API working in Play. 4 | tags=playframework,REST,scala,seed 5 | authorTwitter=amitdevr -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Simple REST API in Play", 3 | "description": "An activator template for implementing Json based REST API.", 4 | "repository": "https://github.com/amitdev/simple-rest-scala", 5 | "addons": [], 6 | "success_url": "/books", 7 | "keywords": ["play", "scala"] 8 | } 9 | -------------------------------------------------------------------------------- /app/controllers/Application.scala: -------------------------------------------------------------------------------- 1 | package controllers 2 | 3 | import play.api.libs.json._ 4 | import play.api.mvc._ 5 | import models.Book._ 6 | 7 | class Application extends Controller { 8 | 9 | def listBooks = Action { 10 | Ok(Json.toJson(books)) 11 | } 12 | 13 | def saveBook = Action(BodyParsers.parse.json) { request => 14 | val b = request.body.validate[Book] 15 | b.fold( 16 | errors => { 17 | // There's a warning here, but it's not clear if toFlatJson 18 | // should really have been deprecated 19 | // https://github.com/playframework/playframework/issues/5531 20 | BadRequest(Json.obj("status" -> "OK", "message" -> JsError.toFlatJson(errors))) 21 | }, 22 | book => { 23 | addBook(book) 24 | Ok(Json.obj("status" -> "OK")) 25 | } 26 | ) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/models/Book.scala: -------------------------------------------------------------------------------- 1 | package models 2 | 3 | import play.api.libs.json.Json 4 | 5 | object Book { 6 | 7 | case class Book(name: String, author: String) 8 | 9 | implicit val bookWrites = Json.writes[Book] 10 | implicit val bookReads = Json.reads[Book] 11 | 12 | var books = List(Book("TAOCP", "Knuth"), Book("SICP", "Sussman, Abelson")) 13 | 14 | def addBook(b: Book) = books = books ::: List(b) 15 | } 16 | -------------------------------------------------------------------------------- /build.sbt: -------------------------------------------------------------------------------- 1 | name := """simple-rest-scala""" 2 | 3 | version := "1.0-SNAPSHOT" 4 | 5 | lazy val root = project.in(file(".")).enablePlugins(PlayScala) 6 | -------------------------------------------------------------------------------- /conf/application.conf: -------------------------------------------------------------------------------- 1 | # This is the main configuration file for the application. 2 | # ~~~~~ 3 | 4 | # Secret key 5 | # ~~~~~ 6 | # The secret key is used to secure cryptographics functions. 7 | # If you deploy your application to several instances be sure to use the same key! 8 | application.secret="aW4]04RAJ^loOVkAYaENh/Mduu[HrJ:Sg/EX^Nko/oye2M" 9 | 10 | # The application languages 11 | # ~~~~~ 12 | # application.langs="en" 13 | 14 | # Global object class 15 | # ~~~~~ 16 | # Define the Global object class for this application. 17 | # Default to Global in the root package. 18 | # application.global=Global 19 | 20 | # Router 21 | # ~~~~~ 22 | # Define the Router object to use for this application. 23 | # This router will be looked up first when the application is starting up, 24 | # so make sure this is the entry point. 25 | # Furthermore, it's assumed your route file is named properly. 26 | # So for an application router like `my.application.Router`, 27 | # you may need to define a router file `conf/my.application.routes`. 28 | # Default to Routes in the root package (and conf/routes) 29 | # application.router=my.application.Routes 30 | 31 | # Database configuration 32 | # ~~~~~ 33 | # You can declare as many datasources as you want. 34 | # By convention, the default datasource is named `default` 35 | # 36 | # db.default.driver=org.h2.Driver 37 | # db.default.url="jdbc:h2:mem:play" 38 | # db.default.user=sa 39 | # db.default.password="" 40 | 41 | # Evolutions 42 | # ~~~~~ 43 | # You can disable evolutions if needed 44 | # evolutionplugin=disabled 45 | 46 | # Logger 47 | # ~~~~~ 48 | # You can also configure logback (http://logback.qos.ch/), 49 | # by providing an application-logger.xml file in the conf directory. 50 | 51 | # Root logger: 52 | logger.root=ERROR 53 | 54 | # Logger used by the framework: 55 | logger.play=INFO 56 | 57 | # Logger provided to your application: 58 | logger.application=DEBUG 59 | -------------------------------------------------------------------------------- /conf/routes: -------------------------------------------------------------------------------- 1 | # Routes 2 | # This file defines all application routes (Higher priority routes first) 3 | # ~~~~ 4 | 5 | # Home page 6 | GET /books controllers.Application.listBooks 7 | POST /books controllers.Application.saveBook 8 | 9 | # Map static resources from the /public folder to the /assets URL path 10 | GET /assets/*file controllers.Assets.versioned(path="/public", file) 11 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | template.uuid=71358ad9-fc02-4dfa-aa4a-1ae1964e5181 2 | sbt.version=0.13.5 3 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.3") 2 | -------------------------------------------------------------------------------- /tutorial/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Simple REST API in Scala - Activator Template 4 | 5 | 6 |
7 |

Edit build.sbt to set up your own app

8 |

9 | build.sbt contains the build configuration; you'll 11 | want to change the project name and version, to start. 12 |

13 |

14 | This is intended to be a boilerplate to start creating your own Json based REST applications. 15 |

16 |
17 | 18 | --------------------------------------------------------------------------------