An open API service indexing awesome lists of open source software.

https://github.com/javieraviles/spring-boot-redis-rest

API REST boilerplate using Spring Boot and Redis as database
https://github.com/javieraviles/spring-boot-redis-rest

boilerplate docker jedis redis rest-api spring-boot

Last synced: 6 months ago
JSON representation

API REST boilerplate using Spring Boot and Redis as database

Awesome Lists containing this project

README

          

# spring-boot-redis-rest

API REST boilerplate with Spring Boot and Redis as database. No big motivation, just because is fun :)

## TODO
- Add Unit Tests

## Getting started with Spring Boot
The [reference documentation](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/) includes detailed [installation instructions](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-installing-spring-boot)
as well as a comprehensive [``getting
started``](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-first-application) guide. Documentation is published in [HTML](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/), [PDF](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/pdf/spring-boot-reference.pdf) and [EPUB](https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/epub/spring-boot-reference.epub) formats.

## Rest Controller
The class `UserResource.java` contains every endpoint. The code is very self-descriptive.

**AVAILABLE ENDPOINTS**

| method | resource | description |
|:------------------|:------------------|:----------------------------------------------------------------------------------------------|
| `GET` | `/users` | get the collection of users -> 200(OK) |
| `GET` | `/users/:id` | get a user by Id -> 200(OK), 400(wrong id format), 404(no user with such id) |
| `POST` | `/users` | creates a user in the DB -> 201(created) |
| `PUT` | `/users/:id` | updates a user in the DB -> 204(updated), 400(wrong id format), 404(no user with such id) |
| `DELETE` | `/users/:id` | deletes a user from the DB -> 204(deleted), 400(wrong id format), 404(no user with such id) |

## Exception Handling
An Spring Boot advice `UserResourceAdvice.java` has been created for handling exceptions and convert them into desired responses. This way both the Rest Controller and the CRUD service are cleaner as they don't need to contain the error handling.

| exception | response |
|:----------------------------------------------|:--------------------------------------|
| `NotFoundException` | `404 NOT FOUND` |
| `RedisConnectionFailureException` | `500 INTERNAL SERVER ERROR` |
| `IllegalArgumentException` | `400 BAD REQUEST` |

## CRUD service
The CRUD is done in `UserService.java` through a **RedisTemplate**, created as a @bean in `SpringRedisApplication.java`. A RedisTemplate should be created for each entity that will be persisted to Redis.

Redis keys for stored users will follow the format `users:`. That way, the whole collection of users can be fetched through the pattern `users:*`. The code is dead simple and very self-descriptive.

## Redis connection
The connection to Redis is established through a `Jedis client`. A `JedisConnectionFactory` is defined in `SpringRedisApplication.java`, taking Redis hostname and password from application properties.

Notice that a `StringRedisSerializer` has been used for keys, otherwise the marshalling would be done through the default serializer (as happens with stored values since we are not specifying any serializer).

## Local dev environment
**Eclipse IDE** -> Spring Tools for Eclipse IDE definitely useful.

Run local **Redis server** in Docker:
docker pull redis
docker run -d --name redis -p 6379:6379 redis