Skip to content

Latest commit

 

History

History
executable file
·
100 lines (83 loc) · 3.18 KB

File metadata and controls

executable file
·
100 lines (83 loc) · 3.18 KB

Example: User id

The use case realized is an in-memory cache on an application server, storing data associated with user id, in order to avoid a database dip for every request.

The code is dived in two parts a set of REST api for managing the user id and a web fronted that consume the API.

REST API

The REST api is developed using Spring boot with Tomcat as application server, Hibernate as JPA implementation and H2 as the database (it is in memory).

#### REST webservice specification:


Method URL Input Body Return Body Description
GET /users/{id} Not required User JSON object Retrieve information about the user with {id}
HTTP 404 code returned if the {id} is not found
GET /users Not required Array of user JSON objects Retrieve all the users
POST /users User JSON object Nothing Create a new user
POST /users/load Not required Nothing Load 50 users
PUT /users/{id} User JSON object Nothing Update an existing user with {id}
HTTP 404 code returned if the {id} is not found
DELETE /users/{id} Not required Nothing Delete an existing user with {id}
HTTP 404 code returned if the {id} is not found

REST API Structure

The API structure is divided in three layers: DAO, Service, Controller.

  • DAO: Is implemented by Spring Data. The only necessary code is defining the DAO interface UserDAO
  • Service: The N Way Cache is used on this layer in order to avoid unnecessary call to database. This layer is accessible via the UserServices interface. Its implementation is done via CachedUserServices.
  • Controller: RestControlleris defining the endpoints for the REST api. It calls the UserService for delegating the API implementation.

User class describe the model of a user.
Application class is the entry point for Spring boot in order to launch H2 in memory database and the tomcat application server.
NotFoundException is raised when a user is not found for a id.

WEB Frontend

The WEB frontend is an addons to the REST api in order to have an easy way to interrogate them.

The web fronted is developed using AngularJs and Bootstrap for the CSS.
It is an implementation of a basic CRUD operation around a user.