Book Image

RESTful Web Services with Scala

By : Jos Dirksen
Book Image

RESTful Web Services with Scala

By: Jos Dirksen

Overview of this book

<p>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.</p> <p>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.</p> <p>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.</p>
Table of Contents (14 chapters)
RESTful Web Services with Scala
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Setting up Scala and SBT to run the examples


To run the examples provided in this book, we need to install Scala and SBT. Depending on your operating system, different steps need to be taken.

Installing Java

Before we can install SBT and Scala, we first need to install Java. Scala requires at least a Java Runtime version of 1.6 or higher. If you haven't installed Java on your system yet, follow the instructions at http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.

Installing Scala and SBT

Once you have Java installed, installing Scala and SBT is just as easy. To install Scala, just go to http://www.scala-lang.org/download/ and download the binaries for your system. To install SBT, you can follow the instructions at http://www.scala-sbt.org/download.html.

To check whether everything is installed, run the following commands in a terminal:

Joss-MacBook-Pro:~ jos$ java -version
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
Joss-MacBook-Pro:~ jos$ scala -version
Scala code runner version 2.11.6 -- Copyright 2002-2013, LAMP/EPFL
Joss-MacBook-Pro:~ jos$ sbt -v
[process_args] java_version = '1.8.0_40'
# Executing command line:
java
-Xms1024m
-Xmx1024m
-XX:ReservedCodeCacheSize=128m
-XX:MaxMetaspaceSize=256m
-jar
/usr/local/Cellar/sbt/0.13.8/libexec/sbt-launch.jar

[info] Set current project to jos (in build file:/Users/jos/)

To exit SBT, hit Ctrl + C.

Running the examples

Now that you've got Java, Scala, and SBT installed, we can run the examples. You can, of course, run the examples from your IDE (see the next section on how to set up IntelliJ IDEA and Eclipse), but often, using SBT directly is just as easy. To run the examples, take the following steps:

  1. Open a terminal and go to the directory where you've extracted the source ZIP file or cloned the repository.

  2. To test the configuration, we've created a simple HelloWorld example. From the console, execute sbt runCH01-HelloWorld:

    Joss-MacBook-Pro:rest-with-scala jos$ sbt runCH01-HelloWorld
    [info] Loading project definition from /Users/jos/dev/git/rest-with-scala/project
    [info] Set current project to rest-with-scala (in build file:/Users/jos/dev/git/rest-with-scala/)
    [info] Compiling 2 Scala sources to /Users/jos/dev/git/rest-with-scala/chapter-01/target/scala-2.11/classes...
    [info] Running org.restwithscala.chapter1.HelloWorld
    SBT successfully ran HelloWorld, configuration seems ok!
    Press <enter> to exit.
    
    [success] Total time: 18 s, completed Jun 13, 2015 2:34:41 PM
    Joss-MacBook-Pro:rest-with-scala jos$
    
  3. You might see a lot of output when the various dependencies are loaded, but after a while, you should see the message, SBT successfully ran HelloWorld, configuration seems ok!.

  4. All the examples in this book wait for user input to terminate. So, once you're done playing around with the example, just hit Enter to terminate the running program.

In each chapter, we'll see the sbt command we need to execute. If you want to know all the examples you can run, you can also run the sbt alias command, which generates the following output:

Joss-MacBook-Pro:rest-with-scala jos$ sbt alias
[info] Loading project definition from /Users/jos/dev/git/rest-with-scala/project
[info] Set current project to rest-with-scala (in build file:/Users/jos/dev/git/rest-with-scala/)
        runCH01-HelloWorld = ; chapter01/runCH01HelloWorld
        runCH01-EchoServer = ; chapter01/runCH01EchoServer

Besides running the examples directly from the command line, it is also possible to run them from an IDE. In the following section, we'll see how to import the examples in IntelliJ IDEA and Eclipse.