API REST boilerplate using Spring Boot and Redis as database
MIT License
API REST boilerplate with Spring Boot and Redis as database. No big motivation, just because is fun :)
The reference documentation includes detailed installation instructions
as well as a comprehensive getting started
guide. Documentation is published in HTML, PDF and EPUB formats.
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) |
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 |
The CRUD is done in UserService.java
through a RedisTemplate<String, User>, 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:<uuid of user>
. That way, the whole collection of users can be fetched through the pattern users:*
. The code is dead simple and very self-descriptive.
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).
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