Upload and Download Rest API using Spring Boot Reactive WebFlux

Reactive Spring Logo

This is maybe a very simple task, but for those of you that may not be familiar with WebFlux found it difficult to create a Rest API that handles upload and download in Reactive spring application.

Create the skeleton Spring project

Create a new project, go to start.spring.io, on the dependencies section, add Spring Reactive Web. Generate and extract it into your desired project location.

Instead of using spring-boot-starter-web, use spring-boot-starter-webflux instead in your pom.xml. Here’s the pom.xml for our Reactive Rest API project.

Create our Rest controller

Now that you have set to use WebFlux instead of the usual web, we have to change a little bit in the Controller class that handles the API endpoints. It doesn’t change much but you have to wrap the Response in Mono and the request parameter to FilePart.

So our upload and download file controller will look like this.

Create utility to handle file upload

We need to structure our project better, so we create a reusable utility that reads the files from user upload and store it into our desired location on the server.

Upload folder configuration

Because we need a configurable file upload folder as in @Value("${file.upload-dir}"), you have to add that config on your application.yml or application.properties. I preferred Yaml, so here's what my application.yml looks like.

On the project folder, create a folder for storing the upload files, create a folder named “upload”.

Write integration tests

That’s pretty much all you need to create a download and upload file Rest API in Reactive web application.

Next step we create integration tests for upload file and download file. 

Postman test

Or you can also create an HTTP request on Postman. With request type POST form-data. And specify the file you want to upload.

Github Project Repo

As always, I have created a git repository for this project available on github. Check the project here, Github repo: Spring-webflux-upload.

Benefits of WebFlux

If you do a performance test for both Reactive and Non-Reactive spring application, it have different performance gap, like in this blog Spring reactive vs non-reactive.

Spring WebFlux can handle 2460 requests per seconds as in MVC you decrease it to 1420 requests per seconds. So Reactive is definitely better than non-Reactive. 

I do a performance test on this project, to upload and download files from this Rest API, but I don’t compare it to non-Reactive and the result is that Reactive is proven to be better.

Starting from now, usually when I create a new Spring Rest API, I will do it in Reactive way, you have better performance. Spring WebFlux performance is way faster 60% compared to Spring MVC and it also has a less error rate.

Popular posts from this blog

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans

Spring Kafka - how to use ReplyingKafkaTemplate send and reply synchronously

MIME Types - Complete List