You are previewing RESTful Web Services with Scala.
O'Reilly logo
RESTful Web Services with Scala

Book Description

Learn the art of creating scalable RESTful web services with Scala

About This Book

  • This is the only book on the market that will help you create scalable RESTful web services using five popular Scala-based REST frameworks

  • Quickly identify the best framework for a specific problem and select the most appropriate solution to suit your requirements

  • This practical guide will help you implement a complete REST-based API from scratch

  • Who This Book Is For

    If you are a Scala developer with some Scala experience and you want to get an overview of the frameworks that are available in the Scala world, then this book is perfect for you. You need to have a general knowledge of REST and Scala. This book is great for senior Scala (or other language) developers who are looking for a good REST framework to use together with Scala.

    What You Will Learn

  • Set up a development environment to easily develop and test REST services

  • Discover the different ways in which you can create REST services with Scala

  • See and explore the different approaches taken by popular REST frameworks

  • Get to know more about a variety of available Scala frameworks such as Finch, Unfiltered, Scalatra, Akka-HTTP, and Play

  • Discover how to create a REST service on top of each framework discussed in this book

  • Create HTTP clients using the frameworks in this book

  • Make an informed decision about which framework to use in a specific situation

  • Set up authentication for your REST services

  • In Detail

    RESTful web services are built to work best on the web. Scala provides a rich set of language constructs and advanced frameworks that you can use to create REST services. However, using Scala and these tools can be a complex task. There are many frameworks available and choosing the wrong framework or approach can cost a lot of time and lead to much frustration. By exploring the most popular Scala REST frameworks, you can make sure you choose the right tool.

    RESTful Web Services with Scala begins with a brief explanation of the REST architecture and its implementation in Scala, as well as the impact that REST architecture has on Scala applications. You will understand the advantages of building Scala web services and how existing Scala applications can take advantage of REST. This book will teach developers about the different programming paradigms available in the Scala world to create RESTful services by exploring the most popular Scala-oriented REST frameworks. It discusses the various facets of RESTful web services such as building scalable APIs, working with standards like HTTP and MIME, designing the architecture, securing the web service, and more.

    With this book, you will be able to build RESTful web services with various Scala frameworks such as Finch, Unfiltered, Scalatra, Akka-HTTP, and Play. You will create basic REST services using frameworks and then extend the REST services with custom functionality. By the end of the book, you'll be able to decide which framework is best suited for your requirements. We finish by looking at how we can use a number of advanced features provided by these frameworks, such as security, creating HTTP clients, working with HATEOAS, and more.

    Style and approach

    This book follows a practical approach where we implement a REST API using each of the frameworks discussed. This book is filled with rich examples and code so you can understand and implement the features of every framework.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. RESTful Web Services with Scala
      1. Table of Contents
      2. RESTful Web Services with Scala
      3. Credits
      4. About the Author
      5. About the Reviewer
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with RESTful Web Services
        1. Introduction to the REST framework
        2. Getting the source code
          1. Downloading the ZIP file
          2. Using Git to clone the repository
        3. Setting up Scala and SBT to run the examples
          1. Installing Java
          2. Installing Scala and SBT
          3. Running the examples
        4. Setting up the IDE
          1. Setting up IntelliJ IDEA
          2. Setting up Eclipse
        5. Testing the REST API
          1. Installing Postman
          2. Importing request collection
          3. Testing the REST service
        6. The REST service and model
          1. API description
        7. Summary
      9. 2. A Functional-style REST Service with Finagle and Finch
        1. An introduction to Finagle and Finch
        2. Building your first Finagle and Finch REST service
        3. HTTP verb and URL matching
        4. Processing incoming requests using RequestReaders
        5. JSON support
          1. Argonaut
          2. Jackson and Json4s
        6. Request validation and custom responses
        7. Summary
      10. 3. A Pattern-matching Approach to REST Services with Unfiltered
        1. What is Unfiltered
        2. Your first Unfiltered service
        3. HTTP verb and URL matching
        4. Extracting request parameters and using futures for asynchronous responses
          1. Converting a request to a Task class
          2. Storing a request in the TaskService
          3. Configuring Unfiltered to work with futures
        5. Adding validation to parameter processing
          1. Introducing directives
          2. Adding search functionality to our API
          3. Directives and working with futures
          4. Adding validation to the request parameters
        6. Summary
      11. 4. An Easy REST Service Using Scalatra
        1. Introduction to Scalatra
        2. Your first Scalatra service
        3. Verb and path handling
        4. Add support for futures and simple validation
        5. Advanced validation and JSON support
          1. Add JSON support
          2. Advanced validations
        6. Summary
      12. 5. Defining a REST Service Using Akka HTTP DSL
        1. What is Akka HTTP?
        2. Creating a simple DSL-based service
        3. Working with paths and directives
        4. Processing request parameters and customizing the response
        5. Exception handling and rejections
        6. Adding JSON support
        7. Summary
      13. 6. Creating REST Services with the Play 2 Framework
        1. An introduction to the Play 2 framework
        2. Hello World with Play 2
        3. Working with the routes file
        4. Adding the Future support and output writers
        5. Adding JSON marshalling, validations, and error handling
        6. Summary
      14. 7. JSON, HATEOAS, and Documentation
        1. Working with JSON
          1. Working with Json4s
          2. Working with Argonaut
          3. Working with spray-json
          4. Working with Play JSON
          5. JSON frameworks summary
        2. HATEOAS
          1. Handling media-types
            1. Handling media-types with Finch
            2. Handling media-types with Unfiltered
            3. Handling media-types with Scalatra
            4. Handling media-types with Akka HTTP
            5. Handling media-types with Play 2
          2. Using links
        3. Summary
      15. Index