Chapter 19. Play Framework and Web Services
This chapter demonstrates recipes for working with web services in Scala, including how to handle server-side HTTP requests, how to convert between JSON and Scala objects, and how to write client-side HTTP requests.
In 2021 there are several great libraries for server-side development with Scala, which you can find on the Awesome Scala list. This chapter focuses on the Play Framework (Play) because it’s popular, well supported, and relatively easy to get started with, especially if you’ve used a framework like Ruby on Rails previously.
One important note is that at the time this book was being produced, Play was not yet updated to work with Scala 3. Therefore, the Play examples you’ll see in this chapter use the Scala 2 syntax. That being said, the Play API has been fairly stable going back to 2013 and the release of the first edition of the Scala Cookbook, so these examples are expected to translate well to Play when it’s available for Scala 3.
The initial recipes in this chapter focus on server-side development with Play. These recipes include:
-
Recipe 19.1, creating a first Play project
-
Recipe 19.2, creating a new endpoint, i.e., a URL for a server-side REST service
-
Recipe 19.3, returning JSON from a Play GET request
-
Recipe 19.4, converting a Scala object to JSON
-
Recipe 19.5, converting JSON to a Scala object
Then the last recipes demonstrate techniques that can work with server- or client-side development:
Get Scala Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.