Skip to content

javieraviles/spring-boot-redis-rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 includes detailed installation instructions as well as a comprehensive getting started guide. Documentation is published in HTML, PDF and 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<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.

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

About

API REST boilerplate using Spring Boot and Redis as database

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages