Design Your REST API
In the previous chapter, we added a new chat feature in our app using Akka. Our web application is close to the end. This chapter will add the REST API in our Play framework application.
We will also create a Scala client using the ws library from the Play framework in order to call our REST API. Later in this chapter, we will add Swagger support and embed the Swagger UI in our app.
In this chapter, we will cover the following topics:
- • REST and API design
- • Creating our API with REST and JSON
- • Creating a Scala client
- • Adding validations
- • Adding back pressure
- • Adding Swagger support
Introduction to REST
Representational State Transfer (REST) is an architectural style. It was defined by Roy Fielding in his doctoral dissertation. REST happens over the HTTP 1.1 protocol using HTTP verbs, such as get, post, delete, put, and Uniform Resource Identifier (URI), for instance, /users/profile/1 or sales/cart/add/2.
The REST architecture has the following properties:
- • Simplicity: Pretty much all languages have libraries to manipulate HTTP URIs.
- • Interoperability: REST is language, platform, and OS agnostic.
- • Scalable and Reliable: As REST is based on HTTP, you can use the HTTP server to scale up your application in conjunction with HTTP load balancer, the HTTP caches, and HTTP DNS.
- • Separation of Concerns (SOC): As you have a URI, that's your contract, not the code, underlying backend, or database. This means that you can change the database or language without affecting the code.
- • Client/Server: There is a server that provides the REST interface and the clients, which call the REST endpoints.
Web services that embrace the REST principles are often called RESTful.