In this article, we will present how to create a Spring Boot application for uploading files to the Redis database. Redis is an in-memory data structure store, used as a database, cache, and message broker. Usually, this type of database is used for other purposes but, there are no contraindications for keeping files in it also.
2. Architecture
In the architecture of the application we could distinguish:
persistence layer (Redis),
Spring Boot with REST Controller, Service, and Repository that communicates with Redis.
3. Technology
Spring Boot application will use the following technologies:
Java 8
Spring Boot 2
Redis
Maven 3.6.1
4. Project structure
To upload files to Redis we need:
FileEntity class contains fields with information about a file,
FileRepository is a Spring Data repository used to save and retrieve files to/from the Redis,
FileService class that calls methods from FileRepository,
FilesController used to handle HTTP requests like POST (for uploading files), GET (downloading files),
application.properties is a Spring Boot configuration file used to setup Redis and set file upload size limits,
pom.xml for Maven dependencies.
5. REST API for uploading/downloading files
The Spring Boot application will provide a REST API for:
uploading files to Redis,
downloading files from the database.
URL
Method
Action
/files
POST
Upload a single file
/files/{uuid}
GET
Download uploaded file
Files will be uploaded directly to the Redis with informations like:
The FileEntity class is annotated with @RedisHash that marks Objects as aggregate roots to be stored in a Redis database.
The entity object that will be saved in Redis has the following structure:
8. Data access layer
In the DAO layer, we have an interface FileRepository that extends org.springframework.data.repository.CrudRepository class to activate CRUD operations.
9. Create a service for managing files
The FileService class was introduced to separate logic related to files from the REST controller.
The structure of FileService is simple. For now, it uses for calling methods from FileRepository.
10. REST controller for handing HTTP requests
The FilesController will be responsible for handling HTTP requests.
This class has been marked with the following annotations:
@RestController - annotation is used to treat this class as a REST controller,
@RequestMapping - create a base endpoint to /files URI.
Other annotations used in this class like @GetMapping, @PostMapping for mapping HTTP GET, POST requests with specific class methods:
HTTP Method
Endpoint
Method
POST
/files
upload(...)
GET
/files/{id}
getFile(...)
11. Configuration file
In the application.properties file we created several properties:
We used the following properties:
redis.host - host of Redis,
redis.port - port on which Redis is available,
redis.password - our Redis instance is secured with password, thats why we need for connection with the database,
redis.database - the index of Redis database,
spring.servlet.multipart.max-file-size - maximum file size for each request.
spring.servlet.multipart.max-request-size - maximum size for a multipart requests.
12. Configuration class
To connect with Redis we need to define a JedisConnectionFactory and a RedisTemplate beans.
13. Main Spring Boot starting server class
The main Spring Boot application class that starts the server has the following structure:
14. Testing Application API
To run the Spring Boot server use mvn spring-boot:run command or find the generated jar in the /target folder and type java -jar upload-file-to-redis-0.0.1-SNAPSHOT.jar.
We will use Postman to make some API requests.
14.1. First, let's upload some file
14.2. Next, let's download the uploaded file using a provided URL
15. Conclusion
In this tutorial, we presented how to create a Spring Boot application that will upload files to the Redis memory database.
As usual, the code used in this tutorial is available in our GitHub repository.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}