A clear, simple example of multipart file upload using Reactive Spring
Spring Boot v2.0.3.RELEASE
Coding using reactive functional style (as opposed to annotation-based)
Spring Initializr Dependencies: “Reactive Web”
Notes follow after the code:
- Spring Webflux makes it extremely easy to build reactive web services – but it takes a while to get used to the terms used to enable effective research and documentation lookup
- The spring reactive framework is powered by project reactor – your first stop should be the documentation there.
- Spring includes a number of really helpful “BodyExtractors” and “BodyInserters“. The former allows for easy extraction of information from the client request while the latter helps to build the server response. The hardest part of this exercise was realizing that “BodyExtractors.toMultipartData()” exists!
- Casting the variable “filePart” as FilePart in line 28 of HomeHandler.kt is essential to expose the “transferTo()” method which makes it easy to save the file to disk
- Sample curl command to use the above:
curl -F "[email protected]/home/dvas0004/Downloads/demo.zip" localhost:8080/upload/123
Note the “file” before the equals sign must match the map index used in line 28 of HomeHandler.kt