Chapter 4. Building a REST API for a Stateless Architecture

This chapter will present the following recipes:

  • Binding requests and marshalling responses
  • Configuring the content-negotiation (json, xml, and so on)
  • Adding pagination, filters, and sorting capabilities
  • Handling exceptions globally
  • Documenting and exposing an API with Swagger

Introduction

In this chapter, quite a few changes will be implemented. In fact, this chapter really sets our application development on an acceleration ramp.

Before diving into the code, we need to brush up on a few concepts about REST.

A definition of REST

REST is an architecture style. Its name is an abbreviation for Representational State Transfer. The term was invented by Roy Fielding, one of the principal authors of the HTTP specification. A REST architecture is designed around a few markers:

  • Identifiable resources: Resources define the domain. A resource must be identifiable by a URI. This URI must be as self-explanatory as possible using resource categories and hierarchies. Our resources will be indices snapshots, stock snapshots, historical index data, historical stock data, users, and so on.
  • HTTP as a communication protocol: We interact with resources using a limited number of HTTP methods (GET, POST, PUT, DELETE, HEAD, and OPTIONS).
  • Resource representation: A resource is visualized under a specific representation. A representation usually corresponds to a media type (application/json, application/xml, text/html) and/or a file extension (*.json, *.xml, *.html).
  • Stateless conversations: The server must not keep traces of a conversation. The use of HTTP sessions must be forbidden and replaced by navigating through the links provided with resources (hypermedia). The client authentication is repeated on every single request.
  • Scalability: Stateless design implies easy scalability. One request can be dispatched to one or another server. This is the role of the load balancers.
  • Hypermedia: As we just mentioned, with resources come links, and those links drive conversation transitions.

RESTful CloudStreetMarket

From this chapter on, all of the implemented data retrievals are now handled with REST using AngularJS. We use Angular routing to complete single-page application design (loaded once from the server).There are also a couple of new services that support three new screens about stocks and indices.

The REST implementation is still partial though. We have only implemented data retrievals (GET); we haven't got an effective authentication yet, and hypermedia will also be introduced later on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset